[英]Get id of any div and its childrens returned from php to jquery
Description : 说明:
After some php work at the end,my php returns the following output to the jquery which called this page 在完成一些php工作之后,我的php将以下输出返回给调用此页面的jquery
<div id = "container1">
<div id = "chicken1"></div>
<div id = "soup1"></div>
</div>
<div id = "container2">
<div id = "chicken2"></div>
<div id = "soup2"></div>
</div>
and so on ... 等等 ...
Now in the jquery call back it gets lot of results. 现在,在jquery回调中,它会得到很多结果。 So among the results I want to search for the divs with specific id's or get those id's. 因此,在结果中,我想搜索具有特定ID的div或获取那些ID。 How can I do that. 我怎样才能做到这一点。
eg lets say I want all the id's that say chicken1 , chicken2 and so on .... Is there a way to do that ?? 例如,让我说我想要所有的id都说chicken1,chicken2等等...。有没有办法做到这一点?
Following Is The Code : 以下是代码:
PHP 的PHP
echo '<div id = "container1">
<div id = "chicken1"></div>
<div id = "soup1"></div>
</div>
<div id = "container2">
<div id = "chicken2"></div>
<div id = "soup2"></div>
</div>';
JQUERY JQUERY
$.post("../PHP/get_all_new_msgs_on_profile.php",{arr:data},function(result) { $(result).hide().prependTo("#new_buddy_messages_on_profile").fadeIn("fast").slideDown("slow"); }
Now the $(result) has the divs returned by the php how can I extract the id's of the divs from it ... PLease ignore any typing mistake . thanks :) $.post("../PHP/get_all_new_msgs_on_profile.php",{arr:data},function(result) { $(result).hide().prependTo("#new_buddy_messages_on_profile").fadeIn("fast").slideDown("slow"); }
现在$(result)具有php返回的div,我如何从中提取div的id ...请忽略任何键入错误。谢谢:)
First of all change <div id='container'>
to <div class='container'>
after that try using this. 首先,尝试将其从<div id='container'>
更改为<div class='container'>
。
$('.container [id*=chicken]').each(function(index){
console.log($(this).attr('id'));
});
As mentioned by @Mritunjay your id's must all be unique. 如@Mritunjay所述,您的ID必须全部都是唯一的。 If you need to assign or access multiple elements by name, you should give them a class ( http://www.w3schools.com/tags/att_global_class.asp ) 如果需要按名称分配或访问多个元素,则应给它们提供一个类( http://www.w3schools.com/tags/att_global_class.asp )
For more info see this 欲了解更多信息请参阅本
jQuery selectors: jQuery选择器:
$("#myId")
ID: $("#myId")
$(".myClass")
类别: $(".myClass")
$("element")
- ie $("div")
元素: $("element")
-即$("div")
For a list of all jQuery selectors see this link: http://api.jquery.com/category/selectors/ 有关所有jQuery选择器的列表,请参见以下链接: http : //api.jquery.com/category/selectors/
To get the child element of a specific element, use children()
. 要获取特定元素的子元素,请使用children()
。 An example of its implementation is as follows: 其实现的示例如下:
$(".myClass").children("div"); //Assuming .myClass has a child `div` element
In your case... 就你而言...
HTML 的HTML
<div id = "container">
<div id = "chicken1"></div>
<div id = "soup"></div>
</div>
<div id = "container">
<div id = "chicken2"></div>
<div id = "soup"></div>
</div>
Should be changed to: 应更改为:
<div class = "container">
<div id = "chicken1"></div>
<div class = "soup"></div>
</div>
<div class = "container">
<div id = "chicken2"></div>
<div class = "soup"></div>
</div>
and to get each element with the class .container
assigned, use $(".container")
to get each child element of the class .container
with the various id's beginning with chicken
you can use the following: 并获得与类的每个元素.container
分配,使用$(".container")
以获取类的每个子元素.container
与各个ID的开始与chicken
,你可以使用以下命令:
$(".container").children('*[id^="chicken"]').each(function () {
//Something here
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.