开发者

Using jQuery prev() method to grab text of H2?

开发者 https://www.devze.com 2023-02-21 19:52 出处:网络
Can\'t seem to get this working. Any ideas? I\'m trying to grab the text that appears between <h2></h2> while working with the input field as a starting location.

Can't seem to get this working. Any ideas?

I'm trying to grab the text that appears between <h2></h2> while working with the input field as a starting location.

jQuery:

$(".title input").each(function(){
    alert( $(this).prev('h2').text() );
}

Html:

<div class="tabbertab">
    <h2>English (US)</h2>
    <div class="title">开发者_JS百科
        <div>
            <label for="title">Title *</label>
        </div>
        <input id="title[en_US]" type="text" name="title[en_US]" value="" class="error">
</div>


$(".title input").each(function(){
    alert( $(this).parent().prev('h2').text() );
}


Using .prev() accesses siblings. By adding .parent() to the chain you access siblings of the parent element.

$(".title input").each(function() {
    alert( $(this).parent().prev('h2').text() );
});

http://jsfiddle.net/4BvJc/


jQuery("h2").each(function(i, obj){            
     alert(obj.textContent);                
});

Try something like this

0

精彩评论

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