Javascript 用文本填充div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10756448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fill div with text
提问by RSM
I have a div
:
我有一个div
:
<div id="shoppingBasket">
</div>
Using Javascript or Jquery how would I fill this with the default text:
使用 Javascript 或 Jquery 我将如何用默认文本填充它:
you have no products in your shopping basket.
you have no products in your shopping basket.
What I mean by default text is I would want to change the text after they clicked a product. so maybe the default text in a variable and then included in the div via JavaScript or Jquery, which would allow the variable to be manipulated.
我的意思是默认文本是我想在他们点击产品后更改文本。所以可能是变量中的默认文本,然后通过 JavaScript 或 Jquery 包含在 div 中,这将允许操作变量。
回答by Adil
You can do it using jQuery text()or javascript innerText
您可以使用 jQuery text()或 javascript innerText
With jquery, using jQuery text()
使用 jquery,使用 jQuery text()
$('#shoppingBasket').text("you have no products in your shopping basket.");
With Javascript, using innerText
使用 Javascript,使用innerText
document.getElementById("shoppingBasket").innerText = "you have no products in your shopping basket.";
回答by chucktator
With jQuery this would be:
使用 jQuery,这将是:
$('#shoppingBasket').text("you have no products in your shopping basket.");
with regular javascript:
使用常规 javascript:
document.getElementById("shoppingBasket").innerHTML = "you have no products in your shopping basket.";
回答by Ian
function fillwithtext(text, elementID) {
document.getElementById(elementID).innerHTML = text;
}
How's that?
怎么样?