如何在jQuery中添加div内容

问题描述 投票:1回答:2

我正在尝试将div中的内容添加到jQuery函数中。请看下面:

我要添加的内容:

<div id="year">2020</div>

我想将'#year'div中的'2020年添加到下面在函数中说'2019'的位置:

<div id="getting-started"></div>
$('#getting-started').countdown('2019/12/24', function(event) { // <-- here
        $(this).html(event.strftime('%D %H %M %S'));
});

提前谢谢您:)

jquery countdown
2个回答
1
投票

只需提取#year div的内容,然后使用该值创建字符串

const year = $('#year').text();
$('#getting-started').countdown(`${year}/12/24`, function(event) {
  $(this).html(event.strftime('%D %H %M %S'));
});

1
投票

使用$('#year').text()获取<div id="year">2020</div>的值,并在如下所示的代码中使用它:

工作片段:-

var year = $('#year').text();
$('#getting-started').countdown(
  year +'/12/24', function(event) { 
    $(this).html(event.strftime('%D %H %M %S')); 
  }
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/hilios/jQuery.countdown/2.2.0/dist/jquery.countdown.min.js"></script>

<div id="year">2020</div><br>
<div id="getting-started"></div>
© www.soinside.com 2019 - 2024. All rights reserved.