开发者

Is there a better way to use JQuery to get parent's next elements attribute

开发者 https://www.devze.com 2023-03-29 00:59 出处:网络
I\'ve got a .each() iterating over a set of labels, each of which is in a td in a table, to generate new elements based on the labels. Unfortunately, the text needed for the id (to associate the new e

I've got a .each() iterating over a set of labels, each of which is in a td in a table, to generate new elements based on the labels. Unfortunately, the text needed for the id (to associate the new elements properly with the model), is the argument to the 开发者_JAVA技巧onclick function of the next td. Currently this is how I'm reaching the text, but it seems really messy to me. Is there a better way to do this?

$(this).parent().next().attr('onclick').toString().split("'")[1]

A sample table row:

<tr><td>Someirrelevantstuff</td><td><label title="foo">bar</label></td><td onclick="foobar('importantvalue')">Irrelevantstuf</td></tr>

EDIT: The table is contained in a div. I get the labels and iterate them via

$("#mydiv label").each(.....)


Why not simply attach that data to a rel / data attribute on your label?

 <label rel="importantvalue">
 <!-- or HTML5 way -->
 <label data-text="importantvalue">

Then you can easily query for the data like so:

 $('#myDiv label').each(function()
 {
       var importantData = $(this).attr('rel');
       // OR HTML5 way
       var importantData = $(this).data('text');

       // Do Stuff
 });
0

精彩评论

暂无评论...
验证码 换一张
取 消