[英]Is it possible to put another div or HTML element replacing "Hello World" text?
<body onload="myfunction('target')"><div id="target"> "Hello World" </div></body>
Is It Possible To Put Another Div Or HTML Element Replacing "Hello World" Text?是否可以放置另一个 Div 或 HTML 元素来替换“Hello World”文本? Hello World is a Left To Right Marquee Text Hello World 是从左到右的字幕文本
<script language="javascript">
function myfunction(id) {
var element = document.getElementById(id);
var textnode = element.childNodes[0];
var text = textnode.data;
setInterval(function() {
text = text[text.length - 1] + text.substring(0, text.length - 1);
textnode.data = text;
}, 400)
}
</script>
If you're just trying to replace the text that is in the innerHTML
/ innerText
querying the HTML element
and simply setting it's innerHTML
/ innerText
to the new value will suffice.如果您只是想替换查询HTML element
的innerHTML
/ innerText
的文本,只需将其innerHTML
/ innerText
设置为新值就足够了。
Example: const myElement = document.getElementByTagName('h1') myElement[0].innerHTML = 'new value'示例: const myElement = document.getElementByTagName('h1') myElement[0].innerHTML = 'new value'
if you want to change html inside div and change to another div or text or any html you can do like this you don't need to replace如果您想更改 div 内的 html 并更改为另一个 div 或文本或任何 html,您可以这样做,您不需要替换
$('#target').html('<div class="col-md-2"></div>');
and if you want to replace then you can do like this如果你想更换,那么你可以这样做
$('#target').html().replace("hello",'<div class="col-md-2"></div>');
innerHTML
should do the trick, if you are trying to add html elements:如果您尝试添加 html 元素,则innerHTML
应该可以解决问题:
Here is the working example of your code snippet:这是您的代码片段的工作示例:
https://codebrace.com/editor/b15cd15b3 https://codebrace.com/editor/b15cd15b3
Example:例子:
...
element.innerHTML = "<span style='color: red; background-color:yellow;'>" + text + "</span>";
...
html code html代码
<body onload="myfunction()">
<div id="target"> Hello World </div>
</body>
javascript code javascript代码
function myFunction() {
var str = document.getElementById("target").innerHTML;
var res = str.replace("Hello World", "testing");
document.getElementById("target").innerHTML = res;
}
myFunction();
Replacing the helloworld
html to testing
html using onload
function.使用onload
函数将helloworld
html 替换为testing
html。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.