使用 jQuery 同时交换 Div id

发布于 2024-12-12 22:47:23 字数 334 浏览 0 评论 0原文

我有两个 div

    <div id="one">xyz</div>
    <div id="two">abc</div>
    <input id="reverse" type="button" value="Reverse" />

单击按钮时,div 的 id 需要互换。

    $('#one').attr('id', 'two');
    $('#two').attr('id', 'one'); 

显然是假的,有人能帮我解决这个问题吗?

I have two div's

    <div id="one">xyz</div>
    <div id="two">abc</div>
    <input id="reverse" type="button" value="Reverse" />

On click of the button, the the id's of the div needs to get interchanged.

    $('#one').attr('id', 'two');
    $('#two').attr('id', 'one'); 

would obviously be false, can anyone help me regarding this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

來不及說愛妳 2024-12-19 22:47:23

像这样的东西应该有效:

$('#reverse').click(function() {
    var $one = $('#one');
    var $two = $('#two');
    $one.attr('id', 'two');
    $two.attr('id', 'one');
});

演示: http://jsfiddle.net/ambigously/9zNq6/

如果您那么真的很偏执:

$('#reverse').click(function() {
    var $one = $('#one');
    var $two = $('#two');
    $one.removeAttr('id');
    $two.attr('id', 'one');
    $one.attr('id', 'two');
});

演示:http://jsfiddle.net/ambiguously/uQnpY/

Something like this should work:

$('#reverse').click(function() {
    var $one = $('#one');
    var $two = $('#two');
    $one.attr('id', 'two');
    $two.attr('id', 'one');
});

Demo: http://jsfiddle.net/ambiguous/9zNq6/

If you're really paranoid then:

$('#reverse').click(function() {
    var $one = $('#one');
    var $two = $('#two');
    $one.removeAttr('id');
    $two.attr('id', 'one');
    $one.attr('id', 'two');
});

Demo: http://jsfiddle.net/ambiguous/uQnpY/

蓝海似她心 2024-12-19 22:47:23

首先缓存选择器,以便它保存对元素的引用,然后交换 id 属性。

var one = $('#one');
$('#two').attr('id', 'one');
one.attr('id', 'two');

jsFiddle

Cache the selector first so it holds a reference to the element, then swap the id attributes.

var one = $('#one');
$('#two').attr('id', 'one');
one.attr('id', 'two');

jsFiddle.

笙痞 2024-12-19 22:47:23

我认为你不应该以这种方式乱搞 ID;如果是定位问题,使用类变量更有意义。您确定不能使用 addclass/removeclass 来达到您的目的吗?

出于好奇,交换两者的内容就足够了吗?

如果你想切换内容,那会很简单:

    function switch_contents(){
            var one_contents = $('#one').html();
            $('#one').html($('two').html());
            $('#two').html(one_contents);
    }

如果你真的有兴趣实际切换ID,这里有一个切换ID的例子; 使用 jQuery 更改元素的 ID

I don't think you should be messing around with IDs in that fashion; if it is a matter of positioning, using a class variable makes more sense. Are you sure you can't use addclass/removeclass for your purposes?

Out of curiousity, would switching the content of the two be sufficient?

If you wanted to switch the content, it would be pretty simple:

    function switch_contents(){
            var one_contents = $('#one').html();
            $('#one').html($('two').html());
            $('#two').html(one_contents);
    }

If you are really interested in actually switching the IDs, there is an example with switching IDs over here; Changing an element's ID with jQuery

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文