jquery - Find element index by its text -
<div id='xlink'>mc</div>
js
$('#next').click(function() { var xlink = $('#xlink').html(); var links = [ 'qd', 'mc', 'vr', 'lm', 'ms', 'kl', 'yu', 'an' ]; foreach(links, element) { if (element.value==xlink){y = element.index}; alert (y); });
what need:
foreach element in links
array, if element value equal xlink value
y = index of element
. in case should 1
.
get elements text use key, , in array indexof, or more cross browser jquery's $.inarray
, , return index
$('#next').on('click', function() { var links = [ 'qd', 'mc', 'vr', 'lm', 'ms', 'kl', 'yu', 'an' ]; var key = $.trim( $('#xlink').text() ); var y = $.inarray(key, links); alert(y); });
Comments
Post a Comment