我有一个小脚本,它使li
元素可点击并将位置设置为li
. 这运行没有错误,我的两个console.log()
电话报告了我所期望的,但是,当您单击li
(无论哪个li
)时,您将被带到href
最后一个链接的li
. 这对我来说没有多大意义,因为我认为我已经正确处理了范围。
请纠正我的理解,让我知道我哪里出错了。
JavaScript:
$(".homeCTA li").each(function(){
$href = $(this).find("a").attr("href");
console.log($(this)); // displays the expected selector
console.log($href); // displays the correct href
$(this).click(function(){ window.location = $href }); // this is overriding all of the previous passes
});
HTML:
<ul class="homeCTA">
<li class="left">
<img src="first-source.jpg">
<h2><a href="SearchResults.asp?Cat=1833">Yada Yada Yada</a></h2>
</li>
<li class="right">
<img src="second-source.jpg">
<h2><a href="SearchResults.asp?Cat=1832">Try the bisque</a></h2>
</li>
</ul>