javascript - 如何在类标签内显示 "grab"内容并仅使用 javascript 显示内容?

标签 javascript html css

我不确定我的方向是否正确,但我想要实现的目标非常复杂,至少对我来说是这样。所以我正在寻找的是在 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;
}

.newContent { position: fixed; bottom: 10%; right: 50%; font-weight: bold; }

Script:

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/

相关文章:

html - 图像在高分辨率下不会展开整个宽度

CSS3 动画 - 如何隐藏动画直到它进入 Div

javascript - 单击更改顶部位置不执行任何操作

javascript - ReactJs中如何传入函数?

javascript - 包含 DIV 到 "cover"包含的元素

php - 将图像链接到标签并使用复选框选择哪些标签

javascript - 将键盘焦点设置为 <div>

javascript - 如何配置我的 Derby.js 应用程序以使用 LESS CSS 引擎?

javascript - 从下到上动画图像

javascript - countdown.js 错误, "Uncaught ReferenceError: countdown is not defined"