.replaceWtih()jquery用于链接标记

时间:2015-08-20 14:24:30

标签: jquery

我试图替换

<link rel="stylesheet" type="text/css" href="content.css">

<!--link commented out for this page only-->

使用jquery .replaceWith();

这行代码位于head标签中。我一直在尝试几种不同的方式,到目前为止它还没有奏效。我只需要在单击按钮时用注释替换一行代码。

这是我到目前为止所写的内容,但它并没有按我想要的方式工作。

$( "<link rel="stylesheet" type="text/css" href="content.css">" ).replaceWith( "<!--link commented out for this page only-->" );

1 个答案:

答案 0 :(得分:0)

您似乎想要操作原始HTML内容。但是,您希望使用选择器来定位DOM上的元素,如下所示:

$( 'link[href="content.css"]' )
.replaceWith( "<!--link commented out for this page only-->" );

注意:请记住,这个代码在DOM加载后运行,很可能会加载CSS。因此,我不确定注释掉这一行会从内存中卸载CSS:(