【发布时间】:2009-12-04 05:30:06
【问题描述】:
我有一个 html 锚元素:
<a title="Some stuff here">Link Text</a>
...我想获取标题的内容,以便将其用于其他用途:
$('a').click(function() {
var title = $(this).getTheTitleAttribute();
alert(title);
});
我该怎么做?
【问题讨论】:
标签: jquery
我有一个 html 锚元素:
<a title="Some stuff here">Link Text</a>
...我想获取标题的内容,以便将其用于其他用途:
$('a').click(function() {
var title = $(this).getTheTitleAttribute();
alert(title);
});
我该怎么做?
【问题讨论】:
标签: jquery
$('a').click(function() {
var title = $(this).attr('title');
alert(title);
});
【讨论】:
prop 用于属性(如disabled、readonly 等)attr 用于属性(attribute=value)。属性不包含值,它们单独存在时是活动的,即<input disabled/> 属性包含值<input attribute=value/>
$('a').click(function() {
var title = $(this).attr('title');
alert(title);
});
【讨论】:
$(this).attr("title")
【讨论】:
你可以简单地在函数内部使用this.title
$('a').click(function() {
var myTitle = $(this).attr ( "title" ); // from jQuery object
//var myTitle = this.title; //javascript object
alert(myTitle);
});
注意
使用另一个变量名而不是“警报”。 Alert 是一个 javascript 函数,不要将其用作变量名
【讨论】:
您可以创建函数并从 onclick 事件中传递此函数
<a onclick="getTitle(this);" title="Some stuff here">Link Text</a>
<script type="text/javascript">
function getTitle(el)
{
title = $(el).attr('title');
alert(title);
}
</script>
【讨论】:
如果你想捕获对文档的每次点击并获取属性值,你也可以试试这个:
$(document).click(function(event){
var value = $(event.target).attr('id');
alert(value);
});
【讨论】: