如何准备好用javascript更改文本?例如id div1 = YES然后div2显示“你说是
<div class="div1">YES</div>
<div class="div2"></div>
答案 0 :(得分:0)
$(function(){
var text = $('.div1').text();
if(text == "YES")
$('.div2').text("You said yes");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div1">YES</div>
<div class="div2"></div>
答案 1 :(得分:0)
如果我理解正确,会发生这样的事情吗?
document.addEventListener('DOMContentLoaded',function(){
var div1 = document.getElementById('div1');
var div2 = document.getElementById('div2');
if(div1.innerText.toLowerCase() == 'yes') {
div2.innerHTML = 'You said YES';
}
});
答案 2 :(得分:0)
您撰写了有关ID的信息,但代码中的内容为class
。
<div id=div1>YES</div>
<div id=div2></div>
<script>
var div1 = document.getElementById('div1');
var div2 = document.getElementById('div2');
if (div1.innerHTML == 'YES') {
div2.innerHTML = 'You said YES';
}
</script>
或者一般来说,如果你想把第二个div'你说'和第一个div的内容放进去。
<div id=div1>XY</div>
<div id=div2></div>
<script>
var div1 = document.getElementById('div1');
var div2 = document.getElementById('div2');
div2.innerHTML = 'You said ' + div1.innerHTML; // returns 'You said XY'
</script>