Jquery从动态元素获取id或类 [英] Jquery get id or class from dynamic element
问题描述
说明如下:
元素1 class = element id = id_1
元素2 class = element id = id_2
元素3 class = element id = id_3
元素4 class = element id = id_4
元素5 class = element id = id_5
我们使用 jquery event 知道他们的类或 id ,但在这种情况下,我们不知道他们的 id 。
$(#id_3)。click(function()
{
//在这种情况下,我们知道我们想要添加事件,当我们点击id_3
});
如何处理PHP查询中的动态元素?
例如,我们如何知道我们点击元素3
与 id_3
?
我们必须填写$ code $(????)click(); ?
当我使用类,我怎么知道哪个
这是我唯一可以得到的方式工作。例如,如果您想获取属性ID或已点击的元素的值...
$( containerElem)on(click,someElemID,function(evt){
var getElemID = $(evt.target).attr(id);
var getVal = $ evt.target).val();
});
Let say I have 5 element from PHP query (so it is dynamic) Illustrated below:
element 1 class=element id=id_1
element 2 class=element id=id_2
element 3 class=element id=id_3
element 4 class=element id=id_4
element 5 class=element id=id_5
We ussulay use jquery event by knowing their class or id, but in this case, we don't know exactly their id.
$("#id_3").click(function()
{
//in this case we have known we want to add event when we click id_3
});
How to deal with dynamic element from PHP query?
For example, how can we know that we click on element 3
with id_3
?
What must we fill in $(????).click();
?
When I use class, how can I know which id I reference from the class clicked?
This was the only way I could get it to work. For example, if you wanted to get the attribute ID or the value of the element that has been clicked...
$("containerElem").on("click", "someElemID", function(evt) {
var getElemID = $(evt.target).attr("id");
var getVal = $(evt.target).val();
});
这篇关于Jquery从动态元素获取id或类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!