开发者

JQuery - Check if next element of DOM has some classname

开发者 https://www.devze.com 2023-01-17 05:13 出处:网络
somethings somethings somethings somethings i need a function that check th开发者_如何学JAVAe next element in the dom and get the classname.
somethings somethings somethings somethings

i need a function that check th开发者_如何学JAVAe next element in the dom and get the classname. For example, if i get sideline0, the function will return "trackon" (classname of the next element). If i get sideline1, it must return "sideone" and so on..

any idea? cheers


How 'bout:

var clz = $('#sideline0').next()[0].className;

Live example: http://jsbin.com/esofu3 (in that example, I gave next the parameter "div" to say I only wanted to look at divs — handy if you want to skip chaff, but not necessary with your sample code).

That uses next to get the next element, and then boring old JavaScript and DOM to get the reflected version of the class attribute. (Or you can use jQuery's attr to retrieve the class attribute instead if you prefer, as babtek suggested.)


You can use next() and attr like this:

alert($('selector').next().attr('class'));

Since you have not mentioned an event, here is sample code for click event of those divs (you can modify as per your needs):

$('.tlcontent > div').click(function(){
  alert($(this).next().attr('class'));
});


Using #sideline0 as the example element, this should get you the class value of the next DOM element:

$('#sideline0').next().attr('class')


USE

$('#sideline0').next().attr('class');

here is a working example http://jsfiddle.net/yCqkC/


It's hard to tell for your specific example because your code is hidden (put 4 spaces before each line so the software reads it as code). However, in general, you can get the classname of the next DOM element as follows:

var className = $('#sideline0').next().attr('class');
0

精彩评论

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