简体   繁体   English

jQuery到Javascript等效项

[英]jQuery to Javascript equivalent

I'm new to JavaScript and also jQuery and have been trying to convert this script: 我是JavaScript和jQuery的新手,并且一直在尝试转换此脚本:

$(document).ready(function () {
    $('#var').val('value');
    document.forms[0].submit();
});

to use only JavaScript, can anyone explain the equivalent functions in js? 只使用JavaScript,谁能解释js中的等效功能?

Use DOMContentLoaded for equivalent of ready . 使用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. 当初始HTML文档已完全加载和解析时,无需等待样式表,图像和子帧完成加载,就会触发DOMContentLoaded事件。 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. 对于人们来说,使用负载是一个非常普遍的错误,而DOMContentLoaded更合适,因此要谨慎。

Use getElementById for id selector. getElementById用作ID选择器。

Returns a reference to the element by its ID. 通过其ID返回对该元素的引用。

Javascript Code: JavaScript代码:

document.addEventListener("DOMContentLoaded", function (event) { // Equivalent of ready
    document.getElementById('var').value = 'value';
    document.forms[0].submit();
});

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

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