将 img 替换为 div jQuery
我正在寻找将带有类的图像替换为带有文本的 div,我该怎么做?
<script>
$('img.vCSS_img_history_off').replaceWith('<div id="button">Turn History Off</div>');
</script>
我知道代码搞砸了,但这只是为了描绘我正在寻找的东西,有什么建议吗?
I'm looking to replace an image with a class to a div with text inside it, how can I do that?
<script>
$('img.vCSS_img_history_off').replaceWith('<div id="button">Turn History Off</div>');
</script>
I know that code is screwed up, but it's just to paint a picture of what I'm looking for, any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在搜索其中的元素之前,您需要确保 DOM 已准备就绪:
或者,您可以在图像后面添加所需的任何内容,然后将其删除:
示例。
有关
.after()
的更多信息此处以及有关.remove 的更多信息()
此处。You need to make sure the DOM is ready before you go searching for elements in it:
Alternatively, you can add whatever you need to after the image and then remove it:
Example.
More on
.after()
here and more on.remove()
here.另一种方法是使用它,但它会删除其他父母的孩子(正如@Vide Simas所说):
你写的看起来不错,我用
replaceWidth
写了一个jsfiddle:http://jsfiddle.net/ydZg5/错误一定是在其他地方
An alternative could be to use this but it will erase other parent's child (as @Vide Simas said):
What you wrote looks like good, i've written a jsfiddle using
replaceWidth
: http://jsfiddle.net/ydZg5/The error must be somewhere else
下面的代码将用包含文本“这是我的 div 文本”的 div 替换类名为“myImageClassName”的图像:
但是,是的,我必须同意其他人的观点..您的代码看起来不错。
The code below will replace an image with class name "myImageClassName" with a div containing the text "This is my div text":
but yeah, I gotta agree with the others.. your code looks fine.