简体   繁体   English

Javascript document.getElementById('id').innerText 不工作

[英]Javascript document.getElementById('id').innerText not working

I have written a code to track values and display in span tags, values tracking is working but display part not working(innerText, i also tried using value) can someone please help me, thank you我已经编写了一个代码来跟踪值并在 span 标签中显示,值跟踪有效但显示部分不起作用(innerText,我也尝试使用值)有人可以帮助我,谢谢

function goAndSetTotal() {
    var amount = document.getElementById("amount").value;

    if (document.getElementById('gst').checked) {
        document.getElementById('subtotal').innerText = amount;
        document.getElementById('gst_box').innerText =  amount*10/100;
        document.getElementById('total').innerText = amount + (amount*10/100);

    } else {
        document.getElementById('subtotal').innerText = amount;
        document.getElementById('gst_box').innerText =  0;
        document.getElementById('total').innerText = amount;
    }

}

Try innerHTML instead like 尝试使用innerHTML

document.getElementById('subtotal').innerHTML = amount;

Take a look at differences between them, let's say you have following html 看看它们之间的差异,假设您有以下html

<div id="Test"><b>InnerText and InnerHTML</b><div>

innerHTML gives <b>InnerText and InnerHTML</b> innerHTML给出<b>InnerText and InnerHTML</b>

innerText gives InnerText and InnerHTML innerText给出了InnerText and InnerHTML

For more details visit HERE 有关详细信息,请访问此处

try to use innerHTML 尝试使用innerHTML

function goAndSetTotal() {
    var amount = document.getElementById("amount").value;

    if (document.getElementById('gst').checked) {
        document.getElementById('subtotal').innerHTML = amount;
        document.getElementById('gst_box').innerHTML  = amount*10/100;
        document.getElementById('total').innerHTML    = amount + (amount*10/100);

    } else {
        document.getElementById('subtotal').innerHTML = amount;
        document.getElementById('gst_box').innerHTML  = 0;
        document.getElementById('total').innerHTML    = amount;
    }

}

尝试使用innerHTML或确保您的网页包含具有id属性的元素

Make sure the .js file should be at the end of the body tag.确保.js文件应位于 body 标记的末尾。

Then it will work for sure:).那么它肯定会起作用:)。

I noticed that doing something like this does not work:我注意到做这样的事情是行不通的:


var amountContainer = document.getElementById('subtotal');

amountContainer.innerHTML = amount;

So the answer to this is to do simply:所以答案就是简单地做:

document.getElementById('subtotal').innerHTML = amount;

I hope this helps someone.我希望这可以帮助别人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM