繁体   English   中英

将代码从jquery重写为javascript

[英]Rewrite code from jquery to javascript

在我的小部件中,我有以下代码:

$(document).ready(function() {
    $('a[rel!=ext]').click(function() { window.onbeforeunload = null; });
    $('form').submit(function() { window.onbeforeunload = null; });
});

我已将此代码放在许多网页中。 我的问题是,并非我的所有网页都运行jQuery,所以我需要一个JavaScript解决方案。

这在JavaScript代码中会是什么样?

更新:

function showWidget(www){

  var content = '<iframe src="'+www+'" style="border:0px #FFFFFF none; position: fixed;left: 0px;width: 300px; height:100%; top: 30%;z-index: 9999999;" name="myiFrame" scrolling="yes" frameborder="0" marginheight="0px" marginwidth="0px" height="100%" width="100%"></iframe><a style="display:none;" href="http://bedbids.com" rel="ext">BedBids</a>';

var newNode = document.createElement("DIV");  
newNode.innerHTML = content;
document.body.appendChild(newNode); 


window.onbeforeunload = function() {
        return "You're leaving the site.";
    };
   window.onload = function() {
   [].slice.call(document.querySelectorAll('a[rel!="ext"]')).forEach(function(a) {
       a.onclick = function() {
           window.onbeforeunload = null;
       };
   });
   [].slice.call(document.getElementsByTagName('form')).forEach(function(form) {
       form.onsubmit = function() {
           window.onbeforeunload = null;
       };
   });
};

}

这样的事情应该起作用:

window.onload = function() {
   [].slice.call(document.querySelectorAll('a[rel!=ext]')).forEach(function(a) {
       a.onclick = function() {
           window.onbeforeunload = null;
       };
   });
   [].slice.call(document.getElementsByTagName('form')).forEach(function(form) {
       form.onsubmit = function() {
           window.onbeforeunload = null;
       };
   });
};

暂无
暂无

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

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