简体   繁体   English

使用 Jquery 交换 div ID

[英]Swapping div ID's using Jquery

I'm trying to swap the ID's using the following interval.我正在尝试使用以下时间间隔交换 ID。

setInterval(
  function() 
  {
    $("#viewNext").attr("id","viewActual");
    $("#viewActual").attr("id","viewNext");
  }, 2000);

This does work, but only one time.这确实有效,但只有一次。 The interval does run, but it seems Jquery looks at the original ID's as stated when loading the page.间隔确实运行,但 Jquery 似乎在加载页面时查看原始 ID。 Is there a way for Jquery to look at the ACTUAL live id's? Jquery 有没有办法查看 ACTUAL live id?

So, why do this?那么,为什么要这样做呢? Let me clarify that!让我澄清一下! :) viewActual is "on top" of viewNext. :) viewActual 是 viewNext 的“顶部”。 The interval swaps there places (and several other css stuff).间隔在那里交换(和其他几个 css 东西)。 While viewActual is on top i load the next file into viewNext so it is loaded when swapped after 2 seconds (or any other time).当 viewActual 位于顶部时,我将下一个文件加载到 viewNext 中,以便在 2 秒(或任何其他时间)后交换时加载它。

Full Code:完整代码:

$('#viewContainer').append('<div class="viewBox" id="viewActual" style="height: '+screenWidth+'px; width: '+screenHeight+'px;"></div>');   
$('#viewContainer').append('<div class="viewBox" id="viewNext" style="height: '+screenWidth+'px; width: '+screenHeight+'px;"></div>');   

$("#viewActual").load('test/test1.php');
$("#viewNext").load('test/test2.php');

setInterval(
  function() 
  {
    $("#viewNext").attr("id","viewActual");
    $("#viewActual").attr("id","viewNext");
  }, 2000);

The loading isn't functioning now.加载现在不起作用。 I know.我知道。 But i do know that the interval doesnt work because i can see that in the mozilla firefox.但我确实知道间隔不起作用,因为我可以在 mozilla firefox 中看到它。 No reason in developing the load function as long as this doesnt work.只要这不起作用,就没有理由开发加载功能。

Use a data-* attribute to store any desired String to swap使用data-*属性来存储任何想要交换的字符串

 $('#viewContainer').append('<div class="viewBox" id="viewActual" data-swapid="viewNext"></div>'); $('#viewContainer').append('<div class="viewBox" id="viewNext" data-swapid="viewActual"></div>'); $("#viewActual").text('aaaaa'); $("#viewNext").text('bbbbb'); setInterval(() => { $('[data-swapid]').each((i, el) => { const data = el.dataset.swapid; // Cache data value el.dataset.swapid = el.id; // Swap ID into data el.id = data; // Set ID from cache }); }, 1000);
 #viewNext {color: red;}
 <div id="viewContainer"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM