简体   繁体   English

当我们从DOM中添加/删除元素时的DOM行为

[英]DOM behaviour when we append/remove element from DOM

Whenever I have to display a popup, I am used to adding an empty div tag and a button on the web page like 每当我必须显示弹出窗口时,我习惯于在网页上添加一个空的div标签和一个按钮,例如

<div id="popupDiv"></div>
<input type="button" id="popupButton" />

Then I hook on a button click event using jQuery and use ajax to call on the server method which returns me the partial view html content as shown below which I bind to the div element using jQuery html method: 然后,我使用jQuery钩住按钮单击事件,并使用ajax调用服务器方法,该方法将返回部分视图html内容,如下所示,我使用jQuery html方法将其绑定到div元素:

$('#popupButton').click(function () {
            $.ajax({
                cache: false,
                data: {},
                dataType: 'html',
                type: 'GET',
                url: '/Inquiry/Inquiry/GetPopupHtml',
                success: function (data) {
                    $('#popupDiv').html(data);
                },
                error: function (data) {
                    alert('Oops! An error has occured!');
                }
            });
});

I also refer a script file in partial view to add functionality on the popup at the end of my html file. 我还引用了局部视图中的脚本文件,以在html文件末尾的弹出窗口中添加功能。 I add all the javascript inside 我在里面添加了所有的javascript

$(document).ready(function(){
//my code inside script
})

I have the following questions 我有以下问题

Do we really need to write my javascript inside $(document).ready()? 我们是否真的需要在$(document).ready()中编写我的JavaScript?

Does DOM gets reloaded when we append/remove element from DOM or When we add html content within an existing element( popupDiv ) iewhen I am using $('#popupDiv').html(data) to display popup content? 当我们从DOM中添加/删除元素时,还是在现有元素( popupDiv )中添加html内容时,即当我使用$('#popupDiv')。html(data)显示弹出内容时,DOM是否会重新加载?

Do we really need to write my javascript inside $(document).ready()? 我们是否真的需要在$(document).ready()中编写我的JavaScript?

No, provided that you are loading your javascript after the dom is ready. 否,前提是您在dom准备就绪后正在加载JavaScript。 You can either add the script tag near the closing body tag so first the dom will be ready, then it will load the script; 您可以在关闭的body标签附近添加script标签,以便首先准备好dom,然后加载脚本。 else you can also use window.onload . 否则你也可以使用window.onload

Does DOM gets reloaded when we append/remove element from DOM 当我们从DOM中添加/删除元素时,是否会重新加载DOM?

No, if the dom gets reloaded, the entire page will be reloaded. 不,如果重新加载dom,则会重新加载整个页面。 During append/remove, only the delta changes are updated. 在追加/删除期间,仅增量更改被更新。

The document ready is used to delay the script execution until the page has been parsed into the DOM. 准备就绪的文档用于延迟脚本执行,直到页面已解析为DOM。 If you put your scripts at the bottom of your page, you do not need the document ready. 如果将脚本放在页面底部,则不需要准备文档。

As far as if the DOM is "reloaded", no. 至于DOM是“重新加载”的,否。 You are appending elements directly to the dom when you use html() or append(). 使用html()或append()时,将元素直接附加到dom。 jQuery takes the elements and appends them, or if you give it html, it will convert the html to elements within a DOM Fragment, and append that fragment to the DOM. jQuery接受元素并将其附加,或者,如果您给它添加html,它将把html转换为DOM片段中的元素,并将该片段附加到DOM。

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

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