jQuery选择动态创建的html元素 [英] jQuery select dynamically created html element

查看:127
本文介绍了jQuery选择动态创建的html元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的这个问题有很多问题与几乎相似的标题,但你知道我没有找到答案。


我的简单问题是:
我有按钮,当我点击它时,javascript创建模态窗口

There are a lot of asked questions with almost similar titles with this question of mine, but you know I didn't find an answer.

My simple question is: I have button, when I click on it, javascript creates modal window

<div class="aui-dialog">
     html here... 
     <button id="closeButton">Close</button>
</div>

< body> 标记之后。


我可以使用jQuery live 绑定关闭按钮的click事件没有问题:

just after <body> tag.

I can bind click event of close button with no problem using jQuery live:

$("#closeButton").live("click", function() { 
    alert("asdf"); // it calls
    $("body").find(".aui-dialog").remove();
});

我的问题是,我无法选择动态创建的模态窗口div by它的类名。这样我就可以调用jQuery .remove()方法来进行关闭操作。现在我知道了,我必须以另一种方式处理动态元素。


什么方式?

My problem is, I cannot select that dynamically created modal window div by its classname. So that I could call jQuery .remove() method to make close action. Now I know, I must deal with dynamic elements in another way.

What way?

编辑:

我认为提到这一点很重要:

我不自己创建模态窗口,我使用 liferay 门户网站。它内置了javascript框架 AUI YUI )创建该模态窗口。我可以在其视图中创建其中的关闭按钮。


I think it's important to mention this:
I dont' create the modal window myself, I use liferay portal. It has built-in javascript framework AUI(YUI) that creates that modal window. I can just create that close button inside it in its view.

编辑2:

模态窗口div类属性值为: aui-component aui-panel aui-dialog aui-widget-located

推荐答案

在创建模态窗口时创建引用:

var modalWindow = $('<div class="aui-dialog">html here... <button id="closeButton">Close</button></div>');
// later...
modalWindow.remove();

要编辑:

当按钮位于模态窗口内时,通过jQuery的 parent 获取窗口:

Get the window via jQuery's parent when the button is inside the modal window:

$('#closeButton').on('click',function() {
    $(this).parent().remove();
    return false;
});

这篇关于jQuery选择动态创建的html元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆