[英]How to load the content of <h> element using jquery
I have a tag like: 我有一个类似的标签:
<h3>Mobile
<img align="middle" alt="Edit" class="attEditCategory" src="/Images/edit.png">
<img align="middle" alt="Delete" class="attDeleteCategory" src="/Images/delete.png">
</h3>
I want to display the text of h3 ie "Mobile" in edit click button (on alert). 我想在编辑单击按钮(警报时)中显示h3的文本,即“移动”。
$(".attEditCategory").button().on("click", function (event) {});
Please help. 请帮忙。
You can use $(this).parent().text()
to get the text. 您可以使用
$(this).parent().text()
来获取文本。 Or $(this).closest('h3').text()
if there could be more to the hierarchy than shown. 或
$(this).closest('h3').text()
如果层次结构可能比显示的更多)。
Eg: 例如:
$(".attEditCategory").button().on("click", function (event) { alert($(this).parent().text()); });
<link href="http://code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="http://code.jquery.com/ui/1.11.2/jquery-ui.min.js"></script> <h3>Mobile <img align="middle" alt="Edit" class="attEditCategory" src="/Images/edit.png"> <img align="middle" alt="Delete" class="attDeleteCategory" src="/Images/delete.png"> </h3>
If there could be text inside the buttons (eg, button
or a
elements rather than img
), $(this).parent().text()
would include that text. 如果有可能的按钮内的文本(如
button
或者a
元素,而不是img
), $(this).parent().text()
将包括文本。 So in that hypothetical case, it's more difficult (but still quite simple) to get just the text of the element itself and not the text of its children: 因此,在这种假设的情况下,仅获取元素本身的文本而不是其子元素的文本更加困难(但仍然非常简单):
alert($(this).parent().contents().map(function() {
return this.nodeType === 3 ? this.nodeValue : ""; // 3 = text node
}).get().join(""));
$(".attEditCategory").button().on("click", function (event) { alert($(this).parent().contents().map(function() { return this.nodeType === 3 ? this.nodeValue : ""; // 3 = text node }).get().join("")); });
<link href="http://code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="http://code.jquery.com/ui/1.11.2/jquery-ui.min.js"></script> <h3>Mobile <a align="middle" alt="Edit" class="attEditCategory">edit</a> <a align="middle" alt="Delete" class="attDeleteCategory">delete</a> </h3>
SInce the class is a child of the h3
use parent()
to target the <h3>
and text()
to get the text 因为该类是
h3
的子级,所以请使用parent()
定位<h3>
并使用text()
获得文本
$(".attEditCategory").button().on("click", function (event) {
alert( $(this).parent().text());
});
$("h3").text();
Official documentation . 官方文件 。
To access a specific h3
tag with a class or an id you do something like: 要使用类或ID访问特定的
h3
标签,您可以执行以下操作:
$("#foo").text(); //foo is the id
$(".foo").text(); //foo is the class
Simply: 只是:
$('h3').text();
And it returns just: "Mobile"
. 它只返回:
"Mobile"
。 It'll be enough to alert: 足以警告:
alert($('h3').text());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.