使用 JavaScript 将图像添加到页面,该页面也使用延迟加载类型功能

Adding images to a page with JavaScript that also uses a lazy load type feature

本文关键字:延迟加载 功能 类型 添加 图像 JavaScript 使用      更新时间:2023-09-26

我正在尝试使用一些JavaScript将BBB和McAfee徽标添加到我们的结帐页面到"帐单和付款"部分。我写了一行有效的代码,但是,它只有在刷新页面时才有效。由于所有活动都发生在一个页面中,并且每次点击继续时都会重新加载页面,因此我不确定为什么我写的内容不起作用。我做错了什么?

<script type='text/javascript'>
    $(document).ready(function(){
    $("#payment h3").prepend('<style>#trustIcons1{position:absolute;right:0;top:-5px;}</style><div id="trustIcons1"><img src="//cdn.shopify.com/s/files/1/0567/6137/files/bbb-secure-trust.png?17807" alt="" class="fl" /><img class="mfes-trustmark mfes-trustmark-hover" border="0" src="//cdn.shopify.com/s/files/1/0567/6137/files/mcafee-secure-trust.png?17807" title="McAfee SECURE sites help keep you safe from identity theft, credit card fraud, spyware, spam, viruses and online scams" alt="McAfee SECURE sites help keep you safe from identity theft, credit card fraud, spyware, spam, viruses and online scams" oncontextmenu="alert(''Copying Prohibited by Law - McAfee Secure is a Trademark of McAfee, Inc.''); return false;"></div>');
    })();
</script>

我认为您需要在 html 或样式或两者之前加上前缀(现在您预置一个字符串)

$(document).ready(function(){ $("#payment h3").prepend('<div id="trusticons">your other css</div>'); })(); 
$(document).ready(function(){ $("#payment h3").prepend('<style>your css</style>'); })();
$(document).ready(function(){ $("#payment h3").prepend('<style>your css</style><div id="trusticons">your other css</div>'); })();