[英]Copy a value from one element to another
I am trying to copy a value of one span to another where the value of the first span is a number. 我正在尝试将一个跨度的值复制到另一个跨度,其中第一个跨度的值是一个数字。 There are multiple class string divs, i want the span value from the first one in each case. 有多个类字符串div,在每种情况下我都希望从第一个值开始生成span值。
This is my HTML structure that I have to work with 这是我必须使用的HTML结构
<button id="btn">Press it</button>
<div class="object">
<div class="header">
<span class="name">0</span>
</div>
<div class="children">
<div class="string">
<div class="header">
<span class="name">Cheese</span>
</div>
</div>
<div class="string"></div>
<div class="string"></div>
<div class="string"></div>
<div class="string"></div>
</div>
</div>
<div class="object">
<div class="header">
<span class="name">Biscuits</span>
</div>
<div class="children">
<div class="string">
<div class="header">
<span class="name">Cheese</span>
</div>
</div>
<div class="string"></div>
<div class="string"></div>
<div class="string"></div>
<div class="string"></div>
</div>
</div>
<div class="object">
<div class="header">
<span class="name">0</span>
</div>
<div class="children">
<div class="string">
<div class="header">
<span class="name">Cheese</span>
</div>
</div>
<div class="string"></div>
<div class="string"></div>
<div class="string"></div>
<div class="string"></div>
</div>
</div>
This is my code that I have so far, and its not working. 到目前为止,这是我的代码,并且无法正常工作。 Not sure where to go next. 不知道下一步要去哪里。
jQuery(function ($) {
$('#btn').click(function () {
if ( $.isNumeric($('div object div.header span.name').text()) ) {
$('div.object div.children div.string div.header span.name').html($('div.object div.header span.name').html());;
}
});
});
You can use callback function .text()
to return the text value based on condition: 您可以使用回调函数.text()
根据条件返回文本值:
$('#btn').click(function () {
$('.object > .header .name').text(function(i,o){
if($.isNumeric(o)){
return $(this).parent().next().find('.header .name').text();
}
})
});
I would do something like this: 我会做这样的事情:
var text_from_span = $('.object .header span.name').text();
if ($.isNumeric(text_from_span)){
var spans_to_fill = document.getElementsByClassName('string');
for(var i = (spans_to_fill.length - 1); i >= 0; i--){
spans_to_fill[i].innerHTML = text_from_span;
}
}
It would be much easier if you could give an id
to <span>
. 如果您可以给<span>
一个id
会容易得多。 Then you could read the value as 然后您可以将值读取为
var txt = $('#idofspan').text();
and set as 并设置为
$('#otherspan').val(txt);
JS Fiddle: https://jsfiddle.net/Lwwy8909/ JS小提琴: https : //jsfiddle.net/Lwwy8909/
Goes a little like this: 像这样:
With the use of an each loop and match to extract a number with any length of digit from the text. 通过使用each循环和match从文本中提取具有任意位数的数字。
jQuery(function ($) {
$('#btn').click(function () {
$('.object').each(function() {
if ( $.isNumeric($(this).find('.name:first').text().match(/\d+/)) ) {
$(this).find('div.children').find('span.name').text($(this).find('.name:first').text().match(/\d+/));
}
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.