简体   繁体   English

.html() 有没有香草 Javascript 替代品?

[英]Is there a vanilla Javascript alternative for .html()?

I'm wanting to create an opposite affect to <noscript> .我想对<noscript>产生相反的影响。 I don't want the content to load at all if Javascript isn't enabled, which is why I'm not interested in a display:none alternative, which still loads but just hides.如果未启用 Javascript,我根本不希望内容加载,这就是为什么我对display:none不感兴趣,它仍然加载但只是隐藏。

I came across this previous answer which has the desired affect (see updated answer).我遇到了这个具有所需影响的先前答案(请参阅更新的答案)。

HTML: HTML:

<div id="container"></div>
<script type="text/html" id="content">
    <div class="test">HTML goes here</div>
</script>

jQuery: jQuery:

$(document).ready(function() {
    $('#container').html($('#content').html());
});

Is there anyway I can do this with Vanilla Javascript?无论如何我可以用Vanilla Javascript做到这一点吗? I want the contents of the script to render as functional HTML.我希望script的内容呈现为功能性 HTML。

often what is done is to set up your html as:通常所做的是将您的 html 设置为:

<html class="no-js">
<head>
  <script>(function(H){H.className=H.className.replace(/\bno-js\b/,'js');})</script>

  <!-- ... -->

</head>

<!-- ... -->

<div class="js-only">I only show up when js is enabled</div>

<!-- ... -->

</html>

and then have some css which hides that element然后有一些隐藏该元素的css

.no-js .js-only {
    display: none;
}

the javascript replaces no-js with js in the <html> element which causes the div to display only when js is enabled的JavaScript取代no-jsjs<html>被使能时的js这导致div来只显示元件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM