将代码从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;
};
});
};
相关文章:
- 如何用javascript重写html文档
- Javascript:重写函数's原型——糟糕的做法
- 我需要用Javascript重写我的客户端库吗
- JavaScript:重写代码行,使其不使用eval()
- 使用 javascript 重写特定链接,但指向特定目录的链接除外
- 用加载的元素和数组将Javascript重写为DRY
- Javascript重写配置文件
- 使用JavaScript重写HTML元素的内容
- JavaScript重写数组方法
- 如何为JavaScript重写URL结构
- 我如何将此JavaScript重写为PHP
- 如何用纯JavaScript重写这个jQuery搜索过滤函数?
- 用Javascript重写链接
- 将Javascript重写为Python
- 使用JQuery和JavaScript重写链接的默认行为
- 从Javascript重写Java函数
- 我可以用javascript重写input type=text元素的标题吗?
- 用javascript重写react函数组件
- Javascript重写对象属性
- Javascript重写问题类xml