将特定文本从一个 div 复制到另一个 div

发布于 2024-11-02 08:09:13 字数 220 浏览 1 评论 0原文

我希望能够将某些文本从一个 div 复制到另一个 div 中。

该文本是从 Wordpress 插件生成的,它生成 2 个不同的 div,其中第 1 个类称为 .voting inactive,第二个类称为 .voting。

生成的文本是基于评级的,它看起来像:评级 4.5/5,我只想采用 4.5 或生成的任何投票并将其复制到 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 called .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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

心在旅行 2024-11-09 08:09:13

我和@Starx 一起讨论这个问题。但更清楚一点:

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

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

那么 JS

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

希望能有所帮助!

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!

不乱于心 2024-11-09 08:09:13

设法让它工作 - 感谢您的所有回答:-)

<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>

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>
你好,陌生人 2024-11-09 08:09:13

试试这个:

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

Try this:

var vote = $(".voting").text().split("/")[0];
我很坚强 2024-11-09 08:09:13

示例

HTML

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

脚本

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

An example

HTML

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

Script

$(document).ready(function() {
   $("#that").html($("#this").html());
});
紧拥背影 2024-11-09 08:09:13
<a class="hello">Copy It</a>
<a class="goodbye"></a>


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

      $(this).clone().appendTo( ".goodbye" );
    });
})(jQuery);
<a class="hello">Copy It</a>
<a class="goodbye"></a>


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

      $(this).clone().appendTo( ".goodbye" );
    });
})(jQuery);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文