如何在评论中包装标签在Javascript或jQuery中 [英] How To Wrap Tag In Comment In Javascript,Or jQuery

查看:105
本文介绍了如何在评论中包装标签在Javascript或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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆