我不确定我的方向是否正确,但我想要实现的目标非常复杂,至少对我来说是这样。所以我正在寻找的是在 html 类标签中找到内容,并在我想要的任何地方显示这些内容。以一个网站上的在线产品为例,假设标题上的代码是这样写的:
< h2 class="Title">This is a product example< /h2>|| note the space had to be to show the exact code.
Yes, I know that i could just use the variable like:
< h2>$title< /h2>But what if don't have access to it? Is there any chance of getting them details using :getElementByClassName And then display that content? I have been looking all over and I, unfortunately got nothing, therefore decided to ask here.
HTML:
<div id="container"> ABC <br />
<p class="displayThis"> I want this to be displayed again
using only JavaScript </p
</div>
风格:
#container { border: 1px solid; width: 200px; margin-left: 15%; background: #999; border-color: #fc0000; text-align: center; } .displayThis { background: #999; color: #fff; font-weight: bold; }Script:.newContent { position: fixed; bottom: 10%; right: 50%; font-weight: bold; }
var displayThys = document.getElementByClassName("displayThis");
document.write('\<p class="newContent">'+ displayThys +'</p>\ <br />\ ');
这是JsFiddle
最佳答案
HTML
<div id="container"> ABC <br />
<p class="displayThis"> I want this to be displayed again using only JavaScript </p>
</div>
这是脚本
<script>
(function(){
var a = document.getElementsByClassName("displayThis");
for (var i = 0; i < a.length; i++) {
var displayAgain = a[i];
document.write('\<p class="newContent">'+ displayAgain.innerHTML +'</p>\ <br />\ ');
}
})();
</script>
代码本身是不言自明的
关于javascript - 如何在类标签内显示 "grab"内容并仅使用 javascript 显示内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21869542/