我试图在单击按钮时获取span的内容。这是html:JQuery获取span的内容
<div class="multiple-result">
<button class="select-location" id="168">Select</button>
<span>Athens, Greece</span>
</div>
<div class="multiple-result">
<button class="select-location" id="102">Select</button>
<span>Athens, Georgia, USA</span>
</div>
我试图得到它,所以当按钮被点击时,它会得到跨度的值。这是我使用的是什么:
$('button.select-location').live('click', function(event){
// set the span value
var span_val = $(this).parent("span").html();
alert('selecting...' + span_val);
});
但警报始终显示:选择...空
谢谢,这工作。 – Frank
我喜欢这个答案。这几乎是我的内心独白每天都在发声。这值得超过6个upvotes! – WheretheresaWill
投票赞成'永远回头看看你2年前写的代码并呻吟,“我为什么这样做?”' –