javascript Jquery 加载选择一个特定的类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14675354/
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-26 22:17:46  来源:igfitidea点击:

Jquery load select a specific class

javascriptjqueryhtmljquery-load

提问by Jupiter

So I am using Jquery's load function like so,

所以我像这样使用 Jquery 的加载功能,

$('#id').load("http://www.thisiswhyimbroke.com");

I know you can add id selectors after the url, but the content im looking for is not contained within an id, is there a way I can select a particular class name?

我知道您可以在 url 后添加 id 选择器,但是我要查找的内容不包含在 id 中,有没有办法可以选择特定的类名?

Even further, if I could select the first li item, then select the class with name item within it, is that possible?

更进一步,如果我可以选择第一个 li 项目,然后选择其中包含名称项目的类,这可能吗?

回答by Denys Séguret

You can provide any selector, for example

您可以提供任何选择器,例如

$('#id').load("http://www.thisiswhyimbroke.com li:first .someclass");

From the documentation:

文档

Loading Page Fragments

The .load() method, unlike $.get(), allows us to specify a portion of the remote document to be inserted. This is achieved with a special syntax for the url parameter. If one or more space characters are included in the string, the portion of the string following the first space is assumed to be a jQuery selector that determines the content to be loaded.

加载页面片段

.load() 方法与 $.get() 不同,它允许我们指定要插入的远程文档的一部分。这是通过 url 参数的特殊语法实现的。如果字符串中包含一个或多个空格字符,则字符串中第一个空格后面的部分将被假定为 jQuery 选择器,用于确定要加载的内容。