从jQuery中的标签获取价值 [英] Get value from a tag in jQuery
问题描述
<a href="?at=privat" at="privat" class="Privat">Privat</a>
我需要一个Jquery来从上面的链接获取私有权限. 我在这里试过了.
I need a Jquery to get the privat from above link. here i Tried .
$(".Privat").click(function(e) {
e.preventDefault();
alert($(this).val());
});
但是它没有返回任何值? 我如何获得价值?
but it didn't returns any value? how can i get the value?
推荐答案
<a>
标记创建一个锚,该锚没有值(通常只有创建输入的标记才有).如果需要其属性之一的值,则可以使用.attr()
函数.
The <a>
tag creates an anchor, which doesn't have a value (generally only tags that create inputs do). If you want the value of one of its attributes, then you can use the .attr()
function.
例如:
alert($(this).attr('at')); // alerts "privat"
如果要获取其文本值(<a>
和</a>
标记之间的内容),则可以使用.text()
函数:
If you want the value of its text (the content between the <a>
and </a>
tags), you can use the .text()
function:
alert($(this).text()); // alerts "Privat"
如果您的HTML有点不同,并且您的<a>
标签包含其他HTML,而不仅仅是文本,如下所示:
If your HTML was a bit different, and your <a>
tag contained other HTML, rather than just text, like this:
<a href="?at=privat" at="privat" class="Privat"><span>Privat</span></a>
然后您可以使用.html()
函数执行此操作(它将返回<span>Privat</span>
).即使将.text()
包裹在一个跨度中,它仍将仅返回"Privat".
Then you could use the .html()
function to do that (it would return <span>Privat</span>
). The .text()
would still just return "Privat" even though it's wrapped in a span.
这篇关于从jQuery中的标签获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!