简体   繁体   中英

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

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

I wish to get the value of <a> element using onclick

and first <li> I wish to get the value of <i> element.

Is it possible?

use 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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