简体   繁体   English

onclick获取元素的这个textContent

[英]onclick get this textContent of an element

<div class="myclass">
<ul>
<li><a> first node <i>textContent</i></a></li>
<li><a>textContent1</a></li>
<li><a>textContent2</a></li>
<li><a>textContent3</a></li>
</ul>
</div>

JS JS

$(".myclass ul li").live("click", function() {
alert(this.textContent);
});

I wish to get the value of <a> element using onclick 我希望使用onclick获取<a>元素的值

and first <li> I wish to get the value of <i> element. 首先<li>我希望获得<i>元素的值。

Is it possible? 可能吗?

use text() : 使用text()

$(document).on("click", ".myclass ul li", function() {
    var txt = $('i', this).length ? $('i', this).text() : $(this).text();
    alert( txt );
});

FIDDLE 小提琴

Also, live() has been deprecated and removed, and to get the text of the i element if such an element exists, you check if exists and the act appropriately 此外, live()已被弃用和删除,并且如果存在这样的元素,则获取i元素的文本,您检查是否存在并且行为是否恰当

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM