Html 如何在div中动态显示文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15413911/
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
how to display text in div dynamically
提问by walkman
I have searched on the forum and saw posts about changing text dynamically upon click. But in my case I want to change the display dynamically when loading the page from beginning. I already have a function that figure out what I should display:
我在论坛上搜索过,看到有关点击时动态更改文本的帖子。但就我而言,我想在从头加载页面时动态更改显示。我已经有一个函数可以确定我应该显示什么:
function phone()
{
//code here
return phone;
}
And my question is how to display the returned phone number in the div below to replace the 1.888.888.8888 part. Can anyone offer some insights? Thank you!
我的问题是如何在下面的 div 中显示返回的电话号码以替换 1.888.888.8888 部分。任何人都可以提供一些见解吗?谢谢!
<div class="add-info">
<span class="rightfloat">Order online <span class="red">or call 1.888.888.8888</span></span>
</div>
回答by talemyn
I would change the HTML to add another <span>
tag around the phone number and give that span tag an id
attribute in order to access it easily (broke it up on separate lines to reduce scrolling):
我会更改 HTML 以<span>
在电话号码周围添加另一个标签,并为该 span 标签提供一个id
属性以便轻松访问它(将其拆分为单独的行以减少滚动):
<div class="add-info">
<span class="rightfloat">
Order online <span class="red">
or call <span id="contact-number"></span>
</span>
</span>
</div>
Then after the page loads update the span
with whatever value you want:
然后在页面加载后span
用你想要的任何值更新:
window.onload = function() {
document.getElementById('contact-number').innerHTML = PHONE_NUMBER_VALUE;
}
In JQuery, it would be:
在 JQuery 中,它将是:
$(document).ready(function () {
$('#contact-number').html(PHONE_NUMBER_VALUE);
});
回答by Alex K.
You can,
你可以,
<body onload="phone();">
<div class="add-info">
<span class="rightfloat">Order online <span class="red">or call
<span id="phone"></span>
</span>
</div>
</body>
And set the value when the function runs;
并在函数运行时设置该值;
function phone() {
document.getElementById("phone").innerHTML = "1.888.888.8888";
}
回答by Niro
Instead of returning 'phone', why don't you put an id on your span and just use
document.getElementById('spanId').innerHTML = phone
in your javascript?
而不是返回“电话”,为什么不在你的跨度上放一个 id 并document.getElementById('spanId').innerHTML = phone
在你的 javascript 中使用
?
回答by Matt Burland
Call you code from the window.onloadevent.
从window.onload事件调用你的代码。
回答by Gatekeeper
I would separate the number into additional <span>
tag with its own id and change content of it with js...
我会将数字分成<span>
带有自己的 id 的附加标签,并使用 js 更改其内容...
document.getElementById('id_of_span').innerText = 'new number';
回答by po228
Try this
尝试这个
<script>
function phone(number) {
var redText = document.getElementByClassname("red")[0];
redText.innerHTML = "or call " + number;
}
</script>
To call it you can use clicks, loads or anything else. For example
要调用它,您可以使用点击、加载或其他任何方式。例如
<script>
window.onload = phone('NEW NUMBER HERE');
</script>
Bear in mind that adding another window onload function later will displace this one, so you would either need to add to it, or use a double delegate function, but that's another story...
请记住,稍后添加另一个窗口 onload 函数将取代这个函数,因此您要么需要添加它,要么使用双委托函数,但那是另一回事了...