开发者

How to add a class in a div somewhere else in the DOM with jQuery?

How can add class .jokjok in the div next of input[bdate]?

Example: http://jsfiddle.net/cLNJE/1/

This code did not work for me:

<button>Click Me</button>
<div class="pa_row mediumCell">
    <div class="column">
        <input type="text" name="expas开发者_开发问答s">
    </div>
    <div class="column">
        <div class="auto_box2">
            <input type="text" name="bdate">
            <div></div>
        </div>
    </div>
    <div class="column">
        <input type="text" name="old">
    </div>
</div>



$('button').live("click", function () {
    $('input[bdate]').closest('.pa_row ').find('auto_box2 div').removeClass().addClass('jokjok');
    //alert('click is ok')
});


I think you need to specify which attribute you want to use for selection of the right input:

$('input[name="bdate"]');

This worked for me:

$('button').live("click", function () {
    $('input[name="bdate"]').closest('.pa_row').find('div.column div.auto_box2 div').first().removeClass().addClass('jokjok');
});

See: http://jsfiddle.net/cLNJE/7/


the easy ways is: Add id attribut to div

<div id="someid"></div>
$('button').live("click", function () {
    $('#someid').addClass('jokjok');
});

Or

Create new whole div

$('button').live("click", function () {
        $('input[name="bdate"]').after('<div class="jokjok"></div>');
    });


Some of your selectors are not quite right,

$('input[bdate]')

should be

$('input[name=bdate]')

this will select the input with a name attribute of 'bdate' your previous selector was selecting all inputs that had an attribute of 'bdate'

find('auto_box2 div')

should be

find('div.auto_box2')

this will select the 'div' with a class of 'auto_box2'

Making those changes leaves us with this code -

$('button').live("click", function() {
    $('input[name=bdate]').closest('.pa_row ').find('div.auto_box2').removeClass().addClass('jokjok');
    alert('click is ok')
});

which should hopefully do what you want.

Updated fiddle - http://jsfiddle.net/ipr101/542ZW/1/


http://jsfiddle.net/cLNJE/8/

$('button').live("click", function () {
    $('.auto_box2 input').siblings('div').addClass('jokjok');
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜