如果我将某些 HTML 注入 DOM 后将使用的 jQuery 放入 $('document').ready() 中,这会是一个问题吗?

发布于 2024-10-04 11:27:46 字数 1051 浏览 8 评论 0原文

我有一个 jQuery 函数,当单击某个按钮时,它会删除某些 html。在单击另一个按钮之前,该 HTML 并不存在于 DOM 中。单击时删除 HTML 的按钮并不存在,直到单击另一个按钮,并且单击时它会自行删除。 $('document').ready() 函数中存在这个问题吗?如果不是的话应该在哪里。

我为什么问是因为我知道我用这个函数以某种方式造成了错误,但不知道在哪里。如果您有任何其他想法,那将会非常有帮助。

代码如下:

<div id="popups">
    <div id="createform">
        <div id="createformInside">
            <input type="text" id="testTitle" size="20">
            <input type="text" id="testSubj">
            <span id="testOptions">More Options</span>
            <br/>
            <textarea id="testContent" ></textarea>
            <input type="button" value="Save Test" id="saveBttn">
        </div>
     </div>
</div>

$('#saveBttn').click( function() {//if the save button on the create test form is clicked...
    $('#createform').remove();//gets rid of the create test form
})

如果有帮助的话,完整的代码在这里: http://jsfiddle.net/chromedude/ggJ4d/

I have a jQuery function that when a certain button is clicked it removes certain html. This HTML does not exist in the DOM until another button is clicked. The button that when clicked removes the HTML does not exist until the other button is clicked and when clicked it removes itself. Is there a problem with this being in the $('document').ready() function? If not where should it be.

Why I ask is because I know I have caused a bug somehow with this function but cannot figure out where. If you have any other ideas it would be very helpful.

Heres the code:

<div id="popups">
    <div id="createform">
        <div id="createformInside">
            <input type="text" id="testTitle" size="20">
            <input type="text" id="testSubj">
            <span id="testOptions">More Options</span>
            <br/>
            <textarea id="testContent" ></textarea>
            <input type="button" value="Save Test" id="saveBttn">
        </div>
     </div>
</div>

$('#saveBttn').click( function() {//if the save button on the create test form is clicked...
    $('#createform').remove();//gets rid of the create test form
})

The full code is here if this would help: http://jsfiddle.net/chromedude/ggJ4d/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

十级心震 2024-10-11 11:27:46

您的问题来自这样一个事实:当文档准备好时,您绑定点击处理程序的 html input 元素不存在。这意味着当您的 .click() 处理程序实例化时它不存在。要将处理程序绑定到现在和将来存在的元素,您可以使用 .live().delegate()。我更喜欢后者,因为它不会绑定到 document 并等待事件冒泡,而是绑定到您传递给它的选择器,并且只监视在该特定范围内触发的冒泡事件元素。

因此,考虑到这一点,您可以像这样修改代码:

$('#popups').delegate("#saveBttn", "click", function() {//if the save button on the create test form is clicked...
    $('#createform').remove();//gets rid of the create test form
});

Your problem comes from the fact that the html input element you are binding a click handler to does not exist when the document is ready. This means it doesn't exist when your .click() handler is instantiated. To bind handlers to elements that exist now and in the future, you can use .live() or .delegate(). I prefer the latter, because it doesn't bind to document and wait for events to bubble up, instead it binds to the selector you pass it, and only watches for bubbling events that get triggered within that specific element.

So with this in mind, you can revise your code like so:

$('#popups').delegate("#saveBttn", "click", function() {//if the save button on the create test form is clicked...
    $('#createform').remove();//gets rid of the create test form
});
揪着可爱 2024-10-11 11:27:46

使用 live 而不是 click,因为在运行时

Live 将用于将来的参考 dom 元素。

$('#saveBttn').live('click', function() {

});

Use live instead of click as you are do that at runtime

Live will be used for future reference dom elements.

$('#saveBttn').live('click', function() {

});
眼泪也成诗 2024-10-11 11:27:46

您可能应该将代码放在生成 HTML 的按钮的 .click() 中。

顺便说一句,您需要关闭 input 标记,例如:

<input type="text" id="testSubj"/>

而不是:

<input type="text" id="testSubj">

基于您的 jsfiddle (http://jsfiddle.net/chromedude/ggJ4d/),类似这样的内容应该有效:

$('#new').click( function() {//if a new test is wanted to be created...
    $('#popups').append($formHtml);

    $('#saveBttn').click( function() {//if the save button on the create test form is clicked...
        $('#createform').empty();//gets rid of the create test form
    })
})

You should probably put the code in the .click() of the button that generates the HTML.

Btw, you need to close the input tags, e.g.:

<input type="text" id="testSubj"/>

instead of:

<input type="text" id="testSubj">

Based on your jsfiddle (http://jsfiddle.net/chromedude/ggJ4d/), something like this should work:

$('#new').click( function() {//if a new test is wanted to be created...
    $('#popups').append($formHtml);

    $('#saveBttn').click( function() {//if the save button on the create test form is clicked...
        $('#createform').empty();//gets rid of the create test form
    })
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文