javascript jquery在<div>中选择多个<p>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5241868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 16:30:01  来源:igfitidea点击:

jquery select multiple <p> in <div>

javascriptjqueryhtmljquery-selectorscss-selectors

提问by whamsicore

HTML:

HTML:

<div id='example'>
  <p> First paragraph</p>
  <p> Second paragraph</p>
  <p> Third paragraph</p>
</div>

Javascript with JQuery: var paragraphs = $('div#examples p');

Javascript 与 JQuery: var paragraphs = $('div#examples p');

This returns an array of HTMLParagraphElement objects. However, I wish to return Jquery objects. (So that I can use e.g:

这将返回一个 HTMLParagraphElement 对象数组。但是,我希望返回 Jquery 对象。(这样我就可以使用例如:

for(i=0;i<paragraphs.length;i++)
{
   paragraph[i].hide();
}

Is there a way I can easily do this? Thanks.

有没有办法可以轻松做到这一点?谢谢。

采纳答案by whamsicore

Thanks everybody for input. Iteration of the div p array was necessary (sorry if that wasn't clear), so doing $('div#example p').hide();was not a proper solution. I ended up doing the following:

感谢大家的投入。div p 数组的迭代是必要的(对不起,如果不清楚),所以这样做$('div#example p').hide();不是一个合适的解决方案。我最终做了以下事情:

var arr = $('div#example p');

for(i=0;i<arr.length;i++)
{
  $(arr[i]).hide();
}

Hope this is useful for people in the future:)

希望这对未来的人们有用:)

回答by xuesong

example:

例子:

$('#examples p').hide();

div is not necessary

div 不是必需的

回答by Chandu

You can still use the the selector query you use. i.e:

您仍然可以使用您使用的选择器查询。IE:

var paragraphs = $('div#examples p');
 paragraphs.hide();

or

或者

 $('div#examples p').hide();

回答by philwinkle

This the the most performant way to query the dom for present issue:

这是查询当前问题的 dom 的最高效方法:

$('#examples).find('p').hide();

$('#examples).find('p').hide();

It's a few more keystrokes, but the selection happens so much faster than the examples given here by others. The reason being is that it traverses all divs first, then finds those that may have the given id, then traverses to find their matching p tags.

这是更多的击键,但选择发生的速度比其他人在此处给出的示例要快得多。原因是它首先遍历所有 div,然后找到那些可能具有给定 id 的 div,然后遍历以找到它们匹配的 p 标签。

In my solution it finds just #examplesand then traverses down to its p tag.

在我的解决方案中,它找到 just#examples然后向下遍历到它的 p 标签。

回答by Vivek

try this...

试试这个...

$('div#examples p').hide();

回答by jairajs89

From the looks of your question the answer would be, as stated by others:

从你的问题的外观来看,答案是,正如其他人所说:

$('div#examples p').hide();

But for the case that you have to iterate through each object that is returned from a jQuery query you should use this syntax:

但是对于您必须遍历从 jQuery 查询返回的每个对象的情况,您应该使用以下语法:

$('div#examples p').each(function(){
    $(this).hide();
});

But remember, if it's as simple as hide, then just use the first example. The second example is when the applied function is specific to each object. The next example is doubling the heights of all returned objects, which could not be done in the same way that the first example is:

但请记住,如果它像隐藏一样简单,那么只需使用第一个示例。第二个示例是当应用的函数特定于每个对象时。下一个示例是将所有返回对象的高度加倍,这无法以与第一个示例相同的方式完成:

$('div#examples p').each(function(){
    var h = $(this).height();
    $(this).height(h * 2);
});