使用jQuery选择div标题

nic*_*2k3 2 html jquery select title

我有一个带有div的网页,其中包含几个没有与之关联的ID的其他div:

<div id="container">
<div   title ="jhon" style=" position:absolute; left: 821px; top: 778.44px; width:8px; height:9px; z-index:3;"> </div> 
<div   title ="carl" style=" position:absolute; left: 561px; top: 579.88px; width:8px; height:9px; z-index:3;"> </div> 
</div>
Run Code Online (Sandbox Code Playgroud)

我需要能够按标题搜索其中一个div,然后访问其样式属性,如left和top.

我正在使用jQuery,但我不知道如何按名称选择元素.我尝试过类似的东西:

var c =$('div[title~="john"]');
alert(c);               //I get [object Object]
alert(c.style.left)     // throws an error and is undefined
Run Code Online (Sandbox Code Playgroud)

但似乎没有用.

有帮助吗?

Gig*_*igi 6

alert( $("div[title=\"john\"]").css("left") );
alert( $("div[title=\"john\"]").css("top") );
Run Code Online (Sandbox Code Playgroud)

或者,如果名称是变量:

var name = "john";
alert($("div[title=\""+name+"\"]").css("left"));
alert($("div[title=\""+name+"\"]").css("top"));
Run Code Online (Sandbox Code Playgroud)