使用jquery获取标签的内容
我想使用jQuery获取标签的内容。例如:使用jquery获取标签的内容,jquery,html,Jquery,Html,我想使用jQuery获取标签的内容。例如: <input type="checkbox" id="checkbox1"> <label for="checkbox1">Get the text of this label</label> 谢谢这应该可以- <label for="checkbox1" id="xyz">Get the text of this label</label> var t = $('#xyz').html()
<input type="checkbox" id="checkbox1">
<label for="checkbox1">Get the text of this label</label>
谢谢这应该可以-
<label for="checkbox1" id="xyz">Get the text of this label</label>
var t = $('#xyz').html();
正如Rory McCrossan已经指出的,这是一个非常基本的jQuery。请注意他提供的链接: var值=$'label'.html; 警戒值;
获取这个标签的文本这真的是jQuery101——你应该能够自己研究的东西。您可能想提及的是,只有当页面上只有一个标签(即第一个标签)时,它才起作用。是的,当您在所有标签上绑定一个事件时,这肯定有效。然而,这不是TS所要求的。而且它在你的答案代码中也不起作用,我的小提琴显示了这一点。这并不是说您的代码是错误的,但我相信您在代码中指出这只适用于页面上的第一个标签,或者在将事件绑定到它时,这一点很重要。回答时尽可能清楚。不需要额外的属性,您可以使用。不知道。对于Javascript,我一点也不擅长,实际上它是最好的,因为它同时使用标签标签和for属性
$('[for=checkbox1]').text();
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(document).ready(function () {
alert($('label[for="checkbox1"]').text())
});
</script>
</head>
<body>
<input type="checkbox" id="checkbox1">
<label for="checkbox1">Get the text of this label</label>
</body>
</html>