我试图让href出现在我的int中,他警告说,但是它返回未定义的.contentUpdate类属于父div,而且我知道这是有效的,因为单击时会出现"new“。但是,在该事件之前的警报中,它返回的是欠罚,而不是链接的href。
我尝试过各种解决方案,比如使用on()方法而不是单击,但也不起作用。我知道这一定是很简单的事情,我就是想不出来,它让我心烦意乱。非常感谢!
HTML
<li data-filtertext="" class="contentUpdate"><a href="#1-1" >Part 1</a></li>
<li data-filtertext="" class="contentUpdate"><a href="#1-2" >Part 2</a></li>脚本(JQUERY)
$(".contentUpdate").click(function() {
/* Act on the event */
//alert("asdfsas")
alert($(this).attr("href");
$("#mainContent").html("new HTML");
});发布于 2018-08-01 02:53:50
问题: $(this)是没有属性href的<li>元素。href属于<a>标记。
解决方案:
$(".contentUpdate").click(function() { alert($(this).children().attr('href')); $("#mainContent").html("new HTML"); });
https://stackoverflow.com/questions/51624487
复制相似问题