简体   繁体   English

如何从JavaScript / jQuery重新呈现页面?

[英]How can you re-render a page from JavaScript/jQuery?

I am not quite sure if that's the correct way to phrase it, but here is my problem 我不太确定这是否是正确的表达方式,但这是我的问题

As you can see, pretty simple code: 如您所见,非常简单的代码:

<div class="first"></div>
<div></div>

What I want to achieve is: 我想要实现的是:

  1. You click on the div with the first class, it will swap that class with the sibling element 单击第一个类的div ,它将与兄弟元素交换该类
  2. You click the sibling element, and it swaps it back, so you just swap classes around 2 elements 您单击兄弟元素,然后将其交换回来,因此您只需在2个元素周围交换类

The problem here is it works correctly only the first time, and the second time when the new element receives the class via addClass , jQuery doesn't recognize that it contains the class by the first page load? 这里的问题是它只在第一次正常工作,第二次新元素通过addClass接收类时,jQuery无法识别它是否包含第一页加载的类? How can I resolve this? 我该如何解决这个问题?

PS: I made a console.log(111); PS:我做了一个console.log(111); just to make sure, and sure enough it triggers ONLY when I click on the black div after the first swap (the one that SHOULD NOT have the first class anymore) 只是为了确保,而且当我在第一次交换后点击黑色div (不应该有第一个类的那个)时,它肯定会触发

To achieve this behavior, you can use delegated events http://api.jquery.com/delegate/ on elements wrapper; 要实现此行为,您可以在元素包装器上使用委派事件http://api.jquery.com/delegate/ ;

$(document).delegate('.first', 'click', function(e){
    e.preventDefault();
    console.log(123);
    $(this).removeClass('first');
   $(this).siblings().addClass('first');
})

A quick and simple way to do it is this: 一个快速而简单的方法是:

 $(document).ready(function() { var first = $('.first'); var second = first.next(); first.click(function(e) { e.preventDefault(); first.removeClass('first'); second.addClass('first'); }); second.click(function(e) { e.preventDefault(); second.removeClass('first'); first.addClass('first'); }); }); 
 div { background-color: black; height: 100px; width: 100px; margin-bottom: 10px; } .first { background-color: green; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="first"></div> <div></div> 

This way does not scale well. 这种方式不能很好地扩展。

Your problem was you only change when you click the $(first) which does not change when clicked it's still point to the first div. 你的问题是你只有在点击$(first)时才会改变,当点击它时它仍然指向第一个div。

A better way with vanilla javascript: 使用vanilla javascript的更好方法:

 document.addEventListener('click', function(e) { if (e.target.classList.contains('first')) { e.target.classList.remove('first') var sibling = getNextSibling(e.target) || getPreviousSibling(e.target) if (sibling) { sibling.classList.add('first') } } }) function getNextSibling(elem) { var sibling = elem.nextSibling while(sibling && sibling.nodeType != 1) { sibling = sibling.nextSibling } return sibling } function getPreviousSibling(elem) { var sibling = elem.previousSibling while(sibling && sibling.nodeType != 1) { sibling = sibling.previousSibling } return sibling } 

All you need to do is push both items into an array, then flip between indexes on click. 您需要做的就是将两个项目都推入一个数组,然后在点击时在索引之间切换。

 var elems = []; $(document).on("click", ".first", function(event) { elems = elems.length == 0 ? [event.originalEvent.target, $(event.originalEvent.target).next()] : elems; $(elems[event.originalEvent.target === elems[0] ? 1 : 0]).addClass("first"); $(elems[event.originalEvent.target === elems[0] ? 0 : 1]).removeClass("first"); }); 
 .first { color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="first">x</div> <div>y</div> 

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

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