用jquery获取标题
grabbing title with jquery
单击<li>
时,我正试图获取它上的"title"。它不断返回undefined。
html
<div id="sidebar">
<div class="navigation">
<ul>
<li title="../../000_Movies/_assets/playlist.html">فیلم ها</li>
</ul>
</div>
</div>
js
$('.navigation li').click(function () {
$('.slider').animate({
marginLeft: 0
}, 500,function() {
var test = $(this).attr('title');
alert(test);
location.href = '';
});
});
这不管用吗?
$('li').click(function(){
alert($(this).attr('title'));
});
"this"可能不是li。或者浏览器有漏洞(IE?)。你的代码对我来说似乎是正确的。
您应该在单击的li
元素上创建一个闭包。您将在单击处理程序函数的另一个函数中获得this
,因此this
的定义将与原始函数不同。
$('.navigation li').click(function () {
// cache the element here
var that = $(this);
$('.slider').animate({
marginLeft: 0
}, 500, function() {
// then access that instead here
// (we're creating a closure on the that variable)
var test = that.attr('title');
alert(test);
location.href = '';
});
});
如果有两个嵌套函数,它们的this
变量将不同:
function foo () {
// this here ...
function bar () {
// ... is different from this here
}
}
所以在此基础上。。。
$('li').click(function () {
// $(this) here ...
$('something').slider({},100, function () {
// ... is different from $(this) here
});
});
$("li").on('click',function () {alert($(this).attr('title');)});
如何在li
元素上附加单击处理程序?您确定处理程序中的this
指向li
元素吗。
我会尝试记录this
并找出它指向什么。
如果this
指向li
,那么$(this).attr('title')
应该工作得非常好。
我猜应该是:
$("li.clickMe").click(function () {
$("this").attr("title").whateverYouNeedToDo("");
});
相关文章:
- Brightcove获取/显示HTML中的当前视频标题和描述
- 在浏览器Cordova/Phonegap中获取页面标题
- 使用Google Feeds API获取RSS Feed标题
- 获取每种语言的跨度标题,并插入新的跨度中
- 如果 document.title 不存在,如何获取页面标题名称
- 使用jquery或javascript在wordpress中获取页面标题
- 如何获取链接内容(innerHTML)并将其作为标题添加到其他链接
- SVG<g>标记获取其<标题>contentJS/jQuery
- jqGrid-如何获取当前网格的标题
- FlexSlider:在右侧垂直获取标题
- 如何编辑此代码以获取图像而不是标题
- 获取打开的选项卡标题 jquery 简单 UI
- jQuery / Javascript方法获取当前页面的标题,将其与字符串匹配,将类分配给该特定元素
- 获取 iFrame 中页面的标题
- 如何在javascript w Ajax和JSON中使用v3 URL API获取Youtube视频标题
- 想要通过GET方法从URL获取值,然后想在我的标题区域的右上角显示这些值
- 如何有效地获取整页标题
- 如何使用javascript从类中的链接获取标题属性
- 如何在 safari 中获取当前页面的 url 和标题
- 如何在应用栏中获取自动完成/文本字段关闭标题