【发布时间】:2014-07-09 23:47:00
【问题描述】:
我希望我的整个 DIV 可以点击,在网上查找一些解决方案,包括下面的解决方案……但无法达到预期的结果..
这是我包含在我的 HTML 文件头中的脚本。
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(".clickable").click(function() {
var href = $(this).attr("data-href");
window.location.href = href;
});
</script>
还有我的 DIV 代码:
<div class="my_account_container">
<span class="my_account_title">Collections
<div class="my_account_sub_container clickable" data-href="http://www.google.com">
<span class="my_account_sub_container_title">My subscriptions</span>
<div class="my_account_counts_container">
<span class="my_account_counts_text">12</span>
</div>
</div>
但我不能让它工作!在此先感谢您的帮助我
【问题讨论】:
-
把你的代码包装在
$(document).ready(function() { .... })-- jsfiddle.net/mblase75/Rmy33 -
你介意展示整个代码的包装吗(这也是我使用 Jquery 的第一天)
-
这样的:
$(document).ready(function(){ $(".clickable").click(function() { var href = $(this).data("href"); window.location = href; }); }); -
如果您是 jQuery 新手,请花一些时间在 learn.jquery.com 上,然后搜索 api.jquery.com 以了解有关具体方法的问题。
-
好的,刚刚看到你的小提琴,这就是解决方案! thx 堆,现在只需要删除“工作!”弹出!
标签: javascript jquery html