开发者

Copy certain text from one div into another div

I want to be able to copy certain text from one div into another div.

The text is generated from a Wordpress Plugin, and it generates 2 different divs, with the following classes 1 called .voting inactive and the second one c开发者_如何学JAVAalled .voting.

The text generated is rating based is it will look something like: Rating 4.5/5, i ONLY want to take the 4.5 or whatever vote is generated and copy that into a div to display the result nicely.

Any help would be great.


I'm with @Starx on this one. But just to be a little more clear:

<div>
    Rating
    <span id="average">4.5</span>/
    <span id="highest_possible">5</span>
</div>

<div id="target">
    <!-- This could be any element -->
</div>

Then the JS

$(document).ready(function() {
   $("#target").html($("#average").html());
});

Hope that helps!


Managed to get it working- thanks for all your answers :-)

<html>
<head>

<style type="text/css">
#target-rating{font-size:30px; text-align:center; font-weight:bold;}
</style>

<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.js"></script>

<script type="text/javascript">
/*$(document).ready(function() { 
       //var arrayOne = $('.voted').html().split('/');
       var arrayOne = $('div[class*="voted"]').html().split('/');
       var arrayTwo = arrayOne[0].split(' '); // arrayTwo[1] contains ie: 4.4
       $('#target-rating').html(arrayTwo[1]);
});*/
</script>

<script type="text/javascript">
// This works for 2 divs
$(function() {
      var rating = $('div.inactive,div.voted inactive').text().replace(/^.*: ([\d\.]+).*$/, '$1'); // Extract the rating
      $('#target-rating').text(rating); // And copy
});
</script>

</head>
<body>
<div id="target-rating"></div>

<!-- Example divs -->
<div class="inactive" id="gdsr_mur_text_796_1">Rating: 4.4/<strong>5</strong> (5 votes cast)</div>
<div class="voted inactive" id="gdsr_mur_text_796_1">Rating: 5.0/<strong>5</strong> (5 votes cast)</div>

</body>
</html>


Try this:

var vote = $(".voting").text().split("/")[0];


An example

HTML

<div id="this">Sometext</div>
<div id="that"></div>

Script

$(document).ready(function() {
   $("#that").html($("#this").html());
});


<a class="hello">Copy It</a>
<a class="goodbye"></a>


(function($) {
    $('.hello').click(function() {

      $(this).clone().appendTo( ".goodbye" );
    });
})(jQuery);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜