我是 JavaScript 和 jQuery 新手,一直在尝试转换此脚本:
$(document).ready(function () {
$('#var').val('value');
document.forms[0].submit();
});
只使用JavaScript,谁能解释一下js中的等效功能吗?
最佳答案
使用DOMContentLoaded
相当于ready
。
The DOMContentLoaded event is fired when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading. A very different event - load - should be used only to detect a fully-loaded page. It is an incredibly popular mistake for people to use load where DOMContentLoaded would be much more appropriate, so be cautious.
使用getElementById
用于 id 选择器。
Returns a reference to the element by its ID.
Javascript代码:
document.addEventListener("DOMContentLoaded", function (event) { // Equivalent of ready
document.getElementById('var').value = 'value';
document.forms[0].submit();
});
关于javascript - jQuery 到 Javascript 的等效项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31209702/