简体   繁体   English

使用jQuery将span元素从一个div移动到另一个div?

[英]Moving a span element from one div to another with jQuery?

I want to move (on page load) the <span>votes</span> part at the bottom and place it inside the .rating-result div: 我想移动(在页面加载时)底部的<span>votes</span>部分并将其放在.rating-result div中:

<div class="topic-like-count average">
 <h4>
  <div style="display: none">UN:F [1.9.10_1130]</div>
  <div class="thumblock ">
   <span class="rating-result">
     <div id="gdsr_thumb_text_43_a" class="gdt-size-20 gdthumbtext">0</div>
  </span>
  <div class="ratingtext ">
  </div>
  <div class="raterclear"></div>
  </div>
 </h4>
<span>votes</span>
</div>

So that the final result looks like this: 所以最终结果如下:

<div class="topic-like-count average">
 <h4>
  <div style="display: none">UN:F [1.9.10_1130]</div>
  <div class="thumblock ">
   <span class="rating-result">
     <div id="gdsr_thumb_text_43_a" class="gdt-size-20 gdthumbtext">0</div>
     <span>votes</span>
  </span>
  <div class="ratingtext ">
  </div>
  <div class="raterclear"></div>
  </div>
 </h4>
</div>

How to accomplish that with jQuery? 如何使用jQuery实现这一目标?

EDIT: 编辑:

Forgot to mention that where is more than one .topic-like-count div (for example): 忘记提到哪里有多个.topic-like-count div(例如):

<div class="topic-like-count good">
 <h4>
  <div style="display: none">UN:F [1.9.10_1130]</div>
  <div class="thumblock ">
   <span class="rating-result">
     <div id="gdsr_thumb_text_43_a" class="gdt-size-20 gdthumbtext">1</div>
  </span>
  <div class="ratingtext ">
  </div>
  <div class="raterclear"></div>
  </div>
 </h4>
<span>votes</span>
</div>

<div class="topic-like-count average">
 <h4>
  <div style="display: none">UN:F [1.9.10_1130]</div>
  <div class="thumblock ">
   <span class="rating-result">
     <div id="gdsr_thumb_text_43_a" class="gdt-size-20 gdthumbtext">0</div>
  </span>
  <div class="ratingtext ">
  </div>
  <div class="raterclear"></div>
  </div>
 </h4>
<span>votes</span>
</div>

(I think I need to use ($this) somewhere) (我想我需要在某处使用($this)

$span=$("#votes").clone();
$("#votes").remove();
$("#gdsr_thumb_text_43_a").append($span);

http://jsfiddle.net/dc47b/4/ http://jsfiddle.net/dc47b/4/

EDIT 编辑

function doIt(){
    setTimeout(function(){
    $span=$("#votes").clone();
    $("#votes").remove();
    $("#gdsr_thumb_text_43_a").append($span);

    }, 1000);
}


window.onload = function() {
     doIt();

   };

http://jsfiddle.net/dc47b/5/ http://jsfiddle.net/dc47b/5/

yet another edit 还有另一个编辑

$(".topic-like-count").each(function(){

$span=$(this).find(".vote").clone();
    $(this).find(".vote").remove();
    $(this).find("#gdsr_thumb_text_43_a").append($span);

});

http://jsfiddle.net/BG7HC/1/ http://jsfiddle.net/BG7HC/1/

and

http://jsfiddle.net/dc47b/6/ http://jsfiddle.net/dc47b/6/

$('.topic-like-count > span').appendTo('.rating-result');

克隆没有意义,你只是在浪费周期。

Use the appendTo keyword. 使用appendTo关键字。

Check out the SO answer How to move an element into another element? 查看SO答案如何将元素移动到另一个元素? .

Also you could I guess use clone and then remove the original 你也可以猜测使用克隆,然后删除原始

To select the actual span in question use something like; 要选择有问题的实际跨度,请使用类似的内容;

$(".topic-like-count:last-child")
$('div.topic-like-count').delegate('span.votes', 'click', function() {
    $('span.rating-result').append($(this));
});

Use delegates, it has a great way of sectioning off logic. 使用委托,它有一个切断逻辑的好方法。

The above code translates to: 以上代码转换为:

  • on every 'div' with 'topic-like-count' class.. (*) 在每个'div'上都有'类似主题'的课程..(*)
  • listen to see if a 'span' object with 'votes' class.. (**) 听看'投票'类的'span'对象..(**)
  • causes a 'click' event. 导致“点击”事件。
  • when that happens, find the 'span' with a 'rating-result' class w/i (*).. 当发生这种情况时,找到带有'rating-result'类的'span'w / i(*)..
  • and add (**) after detaching it from it's original spot 将其从原始位置分离后添加(**)

I used appendTo jquery function for easiest way to moving content 我使用appendTo jquery函数来最简单地移动内容

$mgdiv=$(".your_div_name");
$($mgdiv).appendTo(".target_div_name");

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

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