【发布时间】:2020-02-03 15:02:50
【问题描述】:
我想在 jquery 中按类名获取文本。我已经创建了一个函数来按类名获取元素的文本,但是通过这个函数,我得到了我使用同一个类的三个元素的所有文本。我想获取数组中的文本,例如 x[0] 或 x[1] 等。请帮帮我。
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#hide").click(function(){
myfunction();
});
function myfunction(index) {
var x = $(".check").text();
$("#demo").text(x);}
});
</script>
</head>
<body>
<div id="main">
<p class="check">If you click on the "Hide"</p>
<p class="check">button, I will disappear.</p>
<p class="check">If you click on the "Hide" button</p>
</div>
<p id="demo">"Hide" button</p>
<button id="hide">Hide</button>
<button id="show">Show</button>
</body>
</html>
【问题讨论】:
标签: jquery