如何用jquery删除noscript标签 Package 器?

r1zhe5dt  于 2023-10-17  发布在  jQuery
关注(0)|答案(6)|浏览(132)

我想删除noscribe标签,这是 Package 周围的图像:

<a href="some_image.jpg">
 <noscript>
  <img src="some_image.jpg">
 </noscript>
</a>

我尝试了unwrap(),但它在noscript中不起作用,接下来我尝试了html()方法:

$('a').html(function(index, oldhtml){
   return oldhtml.replace(/\<noscript\\?>/g, '');
});

虽然标签被删除了,但它会产生一个字符串而不是DOM:

<a href="some_image.jpg">
 "
  <img src="some_image.jpg">
 "
</a>

如何在保持img元素不变的情况下删除noscript标签 Package 器?

oalqel3c

oalqel3c1#

你可以用它的内容替换元素:

$('noscript').replaceWith(function() {
    return this.textContent || this.innerText;
    // return $('<div/>').html(this.innerHTML).text();
});

http://jsfiddle.net/x9Eaw/

up9lanfz

up9lanfz2#

尝试类似的东西-这实际上是一个非常粗糙的黑客

jQuery(function($){
    $('a:has(noscript)').html(function(){
        return $(this).find('noscript').text()
    })
})

演示:Fiddle

kwvwclae

kwvwclae3#

你可以试试这样:

var image=$('noscript').text();
$("noscript").remove();
$("a").append(image);

演示Fiddle

rpppsulh

rpppsulh4#

为什么不保留noscript,自己添加图像呢?

$("noscript").each(function () {
    $('<img src="' + this.parentNode.href + '" />').appendTo(this.parentNode);
});

fiddle

kxeu7u2r

kxeu7u2r5#

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML>
<HEAD>
<TITLE>noscript can change the Internet forever</TITLE>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
<!--
$(document).ready(function(){
    $('noscript').replaceWith(function() {
        return this.textContent || this.innerText;
    });
    $("p#javascripton").css("background-color", "yellow"); 
    $("p").click(function(){
        $(this).hide();
    });
}); 
//-->
</SCRIPT>
<noscript>
<p>
Noscript's usage today can be logical for <a href="google.com"><p id="javascripton">eg pc/laptop/high quality tablets usage the complete website with all features:images high resolution,javascript<br><h1>OR without javascript so no high resolutions images inserted with a jquery automated script generated from some php+javascript scripts so have usage for 80% mobile application cause almost are from China ,so low quality products=low cpu,low ram :IN THIS CASE SOMEONE CAN THINK TO SWITCH HIS PHONE TO NO JAVASCRIPT USAGE SO IF ANY PROGRAMMER CAN ADAPT AN ENTIRELY APPLICATION TO THE METHOD I USED IN THIS EXAMPLE AUTOMATED HIS BROWSER IS ADAPT FOR ANY RANDOM ACTION ABOUT THE USER CHOISE(YOU UNDERSTAND "TO USE OR NOT JAVASCRIPT") SO HIS CHINESE PHONE CAN BE APROXIMATELLY APROACH LIKE QUALITY OF SPEED EXECUTION THE OTHERS PC/LAPTOPS/TABLETS QUALITY PRODUCTS.<BR><BR>This stupid example is the best example how no script tag can change the quality of services on this planet ,boost the speed of the internet connection and stops unnecessary use of A LOT OF INTERNET TRAFFIC on slow devices..a simple tag can change the entirely dynamic of programmer's views so entirely Planet's beneficts</h1><p></a> <br>
run this code in two instances :<br>with browser javascript enable <br>and without(browser's javascript disable or eg a firefox plugin noscript states on/off)
</p>
</noscript>
</BODY></HTML>
a14dhokn

a14dhokn6#

为什么我们需要jQuery?

document.querySelectorAll("[unwrap]").forEach(function(a){a=a.textContent;a=document.createRange().createContextualFragment(a);document.body.appendChild(a)});
<a href="some_image.jpg">
 <noscript unwrap>
  <img src="some_image.jpg" alt="some_image">
  <h1>Hello World</h1>
 </noscript>
</a>

希望这对你有帮助!

相关问题