如何在评论中包装标签在Javascript或jQuery中 [英] How To Wrap Tag In Comment In Javascript,Or jQuery
问题描述
我想在运行时禁用一些标签和脚本,
< div id =some-div>
< img src =http://placekitten.com/200/300/>
< img src =http://placekitten.com/200/150/>
< img src =http://placekitten.com/350/330/>
< / div>
$ b $(function()
{
$('img')。wrap('<!--->< / --->') ;
});
但他们不包裹他们删除我的标签
查看演示
< a href =http://jsfiddle.net/p8nbpt7o/1/ =nofollow> http://jsfiddle.net/p8nbpt7o/1/
直接在HTML中
HTML注释以开头<! -
并以 - >
结尾。
如果您想将所有标签注释在一起您的HTML,你可以这样做:
<! -
< img src =http://placekitten.com/200/150/>
< img src =http://placekitten.com/350/330/>
- >
如果您想在HTML中单独评论所有代码,可以这样做:
<! - < img src =http://placekitten.com/200/300> - >
<! - < img src =http://placekitten.com/200/150> - >
<! - < img src =http://placekitten.com/350/330> - >
使用jQuery
如果你想用jQuery来做,你可以使用 replaceWith
而不是 wrap
:
$(function(){
$('img')。replaceWith(function(){
返回<! - + this.outerHTML + - >;
});
});
结果:
<! - < img src =http://placekitten.com/200/300> - >
<! - < img src =http://placekitten.com/200/150> - >
<! - < img src =http://placekitten.com/350/330> - >
我在Chrome,Firefox和IE11上试过了,它对我来说很好!
小提琴: b
http://jsfiddle.net/p8nbpt7o/3/
I Want To Wrap My IN Comment I Want To Disable Some Tags and Scripts In Run Time,
<div id="some-div">
<img src="http://placekitten.com/200/300"/>
<img src="http://placekitten.com/200/150"/>
<img src="http://placekitten.com/350/330"/>
</div>
$(function ()
{
$('img').wrap('<!---></--->');
});
But They Don't Wrap They Delete My Tag See Demo http://jsfiddle.net/p8nbpt7o/1/
Directly in HTML
HTML comments start with <!--
and end with -->
.
If you want to comment all your tags together in your HTML, you could do it like this :
<!--
<img src="http://placekitten.com/200/300"/>
<img src="http://placekitten.com/200/150"/>
<img src="http://placekitten.com/350/330"/>
-->
If you want to comment all your tags individually in your HTML, you could do it like this :
<!--<img src="http://placekitten.com/200/300">-->
<!--<img src="http://placekitten.com/200/150">-->
<!--<img src="http://placekitten.com/350/330">-->
With jQuery
If you want to do it with jQuery, you can use replaceWith
instead of wrap
:
$(function(){
$('img').replaceWith(function() {
return "<!--" + this.outerHTML + "-->";
});
});
Result :
<!--<img src="http://placekitten.com/200/300">-->
<!--<img src="http://placekitten.com/200/150">-->
<!--<img src="http://placekitten.com/350/330">-->
I tried it in Chrome, Firefox and IE11 and it works fine for me!
The Fiddle :
http://jsfiddle.net/p8nbpt7o/3/
这篇关于如何在评论中包装标签在Javascript或jQuery中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!