How to click to get the text value of the current element in jquery

html 

<ul>
	<li>1</li>
	<li>2</li>
	<li>3</li>
	<li>4</li>
	<li>5</li>
</ul>

jquery 

$(function(){
		$("ul").find("li").each(function(i){
			$(this).click(function(){
				$("h2").text($(this).text())
			})
		})
	});
	// 也可以直接这样写
	$(function(){
		$("ul").find("li").click(function(){
			$("h2").text($(this).text())
		})
	});

Expansion:

.html(),.text(),.val() three methods are used to read the content of the selected element; (.value is used in js)

.html() is used to read the html content of the element (including html tags);

.text() is used to read the plain text content of the element, including its descendant elements;

.val() is used to read the "value" value of the form element;

.html() and .text() methods cannot be used on form elements, and .val() can only be used on form elements;

Guess you like

Origin blog.csdn.net/baidu_39043816/article/details/108621727