【问题标题】:AJAX remember generated element styleAJAX 记住生成的元素样式
【发布时间】:2016-07-25 09:34:41
【问题描述】:

我有一个网站,可以在其中生成一些带有数据的 div。我每 60 秒后重新生成这些数据以获取最新数据。我为每个 div 添加了一个全屏按钮,为 div 提供了一个带有 javascript 的 .fullscreen 类。这使得 div fixed position100% width100% height

当 div 重新生成时,不会记住 div 上的 .fullscreen。有什么聪明的方法可以实现这一目标吗?因此 div 得到更新,带有 .fullscreen 的特定 div 再次获得类。

网站是用 ASP.NET 5 MVC 6 制作的。

示例结构

<div id="container">

   <div class="tile">data...</div>
   <div class="tile">data...</div>
   <div class="tile fullscreen">data...</div>
   <div class="tile">data...</div>

</div>

ajax调用后,将容器替换为以下内容(清除.fullscreen类)

  <div id="container">

       <div class="tile">data...</div>
       <div class="tile">data...</div>
       <div class="tile">data...</div>
       <div class="tile">data...</div>

    </div>

【问题讨论】:

  • 您可以使用 javascript 来实现这一点。 $("#container").append( $('
    ').attr('id', 'container').addClass(fullscreen').html('New element') );
  • @Thennarasan 是的。但我需要知道 哪个 div 提供全屏类。
  • 如果你知道你想给什么 div 标签。您可以索引并更新。 $(document.getElementById('container').children[2]).addClass('fullscreen');
  • @Thennarasan 用户按下哪个 div 到全屏是未知的。我需要一种方法来知道children[?] 它是什么。因此,以某种方式记住索引服务器端或客户端交叉 ajax 调用。
  • 很简单..点击即可。 $(function() { //run when the DOM is ready $(".tile").click(function() { //use a class, since your ID gets mangled $(this).addClass("active"); //add the class to the clicked element }); });

标签: ajax


【解决方案1】:

在你的 ajax 成功函数中

//Get position of div where success class is applied
var index_fs=$( "#container > div" ).index($("#container > div.fullscreen"));

// your code  should be here for remove and append all div

// if index of fullscreeen is found means >=0
if(index_fs>=0)
{
    $( "#container > div:eq("+index_fs+")" ).addClass("fullscreen");
}

【讨论】:

  • 我喜欢这个主意。但它不起作用。 index_fs 始终是 `-1`。
  • 对不起。我拼错了一些东西。这完美!谢谢!
猜你喜欢
相关资源
最近更新 更多
热门标签