javascript - jQuery 到 Javascript 的等效项

标签 javascript jquery

我是 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/

相关文章:

jquery - 为什么 jquery 中的 .first() 针对第二个元素?

javascript - 如何根据第一次 HTTP 调用获取的 ID 呈现数据

javascript - 在销毁之前删除数据表中的行

javascript - 使用 JQuery 插入 DOM 时 CSS 背景图像无法正确缩放

javascript - 如何遍历 h1 元素并使每个单词使用 jquery 淡入淡出?

javascript - 动态添加元素到灯箱 (Venobox) 画廊

javascript - 表单集表单域中的键盘错误

javascript - 在 react 中渲染嵌套的对象数组

javascript - 使用 Javascript setTimeout 调用 MVC 操作以保持表单例份验证事件

javascript - 用 div 切换值