我正在使用WordPress建网站,因此无法在HTML代码中添加事件onclick。但是我可以添加CSS和JavaScript。
我已将要点击的元素添加了ID,并添加了以下JavaScript代码:
document.getElementById('theID').onclick = function () {
window.location.href = 'link';
}
我不理解为什么它不能正常运作。有其他更好的方法吗?
我正在使用WordPress建网站,因此无法在HTML代码中添加事件onclick。但是我可以添加CSS和JavaScript。
我已将要点击的元素添加了ID,并添加了以下JavaScript代码:
document.getElementById('theID').onclick = function () {
window.location.href = 'link';
}
我不理解为什么它不能正常运作。有其他更好的方法吗?
你不能使用jQuery吗? 如果可以使用jQuery,就像这样绑定事件:
$("#theID").click(function(){
location.href="link";
});
window.onload = function () { document.getElementById("redirectThis").onclick = function () {
location.href = "www.yoursite.com";
}; }
<div class="container">
<button id="redirectThis">Click this to direct it</button>
</div>