不使用innerHTML替换dhtml元素
这看起来应该很容易。我有一个 html 片段,我希望通过 javascript 找到并修改它。但不仅仅是innerHTML;我想替换整个元素。示例:
<div class="content">
<div class="item">
<img src="images/pic1.jpg" />
<a class="clicker" onclick="javascript:doSomethingUseful(##);">Do ##!</a>
<h3>title</h3>
<p>description</p>
</div>
</div>
页面加载后,我想获取 Now!
并将其替换为三个实例,例如:
<div class="content">
<div class="item">
<img src="images/pic1.jpg" />
<a class="clicker" onclick="javascript:doSomethingUseful(1);">Do 1!</a>
<a class="clicker" onclick="javascript:doSomethingUseful(2);">Do 2!</a>
<a class="clicker" onclick="javascript:doSomethingUseful(3);">Do 3!</a>
<h3>title</h3>
<p>description</p>
</div>
</div>
使用prototype.js ,我可以轻松执行 $$('.clicker') 并获取包含该元素的数组。我可以使用 .innerHTML 并获得“Do ##!”并改变它。但我想要,不,需要整个元素才能将其插入到位。我可以经历兄弟姐妹和父母的奇怪阴谋,然后绕着节点走回去,最终得到我需要的东西,我会这么做。看来我在这里缺少一些可以让这变得容易的东西。
This seems like it should be easy. I have a html snippet that I wish to locate and modify in place via javascript. But not just the innerHTML; I want to replace the entire element. Example:
<div class="content">
<div class="item">
<img src="images/pic1.jpg" />
<a class="clicker" onclick="javascript:doSomethingUseful(##);">Do ##!</a>
<h3>title</h3>
<p>description</p>
</div>
</div>
After page load, I want to grab the <a class="clicker" ...>Now!</a>
and replace it with three instances like:
<div class="content">
<div class="item">
<img src="images/pic1.jpg" />
<a class="clicker" onclick="javascript:doSomethingUseful(1);">Do 1!</a>
<a class="clicker" onclick="javascript:doSomethingUseful(2);">Do 2!</a>
<a class="clicker" onclick="javascript:doSomethingUseful(3);">Do 3!</a>
<h3>title</h3>
<p>description</p>
</div>
</div>
Using prototype.js, I can easily do $$('.clicker') and get an array with the element. I can use .innerHTML and get the 'Do ##!' and change it. But I want, nay, need the entire element to insert it in place. I can go through weird machinations of siblings and parent, and walk back around the nodes to eventually get what I need, and I will do that. It just seems that I am missing something here that would make this easy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果它不是您想要在页面中运行的唯一 HTML 生成,您可以考虑使用 javascript 模板引擎。
有几个优点,主要的一个是 HTML 视图和 JS 逻辑之间的明确划分。
有很多这样的引擎可以满足各种口味。
这是使用prototype.js 和 PURE 模板引擎时的样子:
If it is not the only HTML generation you want to run in your page, you may consider a javascript templating engine.
There are several advantages, the main one being a clear cut between the HTML view and the JS logic.
There are plenty of these engines available for every taste.
Here is how it would look with prototype.js and the PURE template engine:
在IE中你可以设置outerHTML。在 FF 中,您可以使用以下命令:
http://snipplr.com/view/5460 /outerhtml-in-firefox/
In IE you can set outerHTML. In FF, you can use this:
http://snipplr.com/view/5460/outerhtml-in-firefox/
所以我这样做的方法是:
clicker
迭代所有位于类div
中的锚点,类item
位于类内部content
所以这是我的 html 块的样子:
我需要包含 Prototype:
还有一个用于保存替换 html 的变量,以及一个虚拟函数,以便我们插入的内容起作用:
然后这是我的代码,您可能会找到它对于了解这些工作原理的背景故事很有用:
$$
< /a>,Element.observe
, < a href="http://www.prototypejs.org/api/element/methods/hide" rel="nofollow noreferrer">Element.hide
,Element.insert
,Element.insert
prototypejs.org/api/event/stop" rel="nofollow noreferrer">Event.stop
:So the way I would do this is:
clicker
that are inside adiv
with classitem
which are inside classcontent
So here's what my html chunk looks like:
And I need to include Prototype:
And a variable to hold your replacement html, and a dummy function so what we insert works:
Then here's my code, you may find it useful to get the backstory on how these work:
$$
,Element.observe
,Element.hide
,Element.insert
,Event.stop
:您可以添加和附加元素。
You can add and append elements.