在jquery中获取span标题

Bha*_*tre 3 jquery-ui

我想在以下html中检索span标题.

<div id="header_customerid_d">
<div>
<span title="This is my span"></span>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)

我试过跟随jquery bt我得到了"未定义"警报.

var CustomerId = $('#header_customerid_d',this).children("div").children("span").attr("title");
alert(CustomerId);
Run Code Online (Sandbox Code Playgroud)

所以请为我提供正确的解决方案.

谢谢,Bharat Mhatre

Jam*_*ice 6

var CustomerId = $("#header_customerid_d span").prop("title");应该做的伎俩.在这里看一个例子.

请注意,该prop功能仅适用于jQuery 1.6+.如果您使用的是旧版本的jQuery,请attr改用:

$("#header_customerid_d span").attr("title");
Run Code Online (Sandbox Code Playgroud)