简体   繁体   中英

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

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 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
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM