most()是jQuery中的内置方法, 该方法返回DOM树中所选元素的第一个祖先。此方法在搜索元素的第一个祖先时从当前元素向上遍历。文档对象模型(DOM)是万维网联盟的标准。这定义用于访问DOM树中的元素

语法如下:

$(selector).closest(para1, para2);

参数:它接受下面指定的两个参数:

  • para1:这指定了在DOM树中叙述祖先搜索的元素。
  • para2:这是一个可选参数DOM元素, 可在其中找到匹配元素。

返回值:它返回所选元素的第一个祖先。

jQuery代码, 以显示mostest()方法的工作方式:

代码示例1:

在下面的代码中, 未传递可选参数。

<html><head><style>.main * {display: block;border: 2px solid lightgrey;color: grey;padding: 5px;margin: 15px;}</style><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><script><!-- here is the script code for performing the method -->$(document).ready(function() {$("span").closest("ul").css({"color": "green","border": "2px solid green"});});</script>
</head><body class="main">This is great-great grand parent element !<div style="width:600px;">This is great grand parent element !<ul>This is the second ancestor element !<ul><!-- This element will be selected -->This is first ancestor element !<li>This is direct parent !<span>This is span the child element !</span></li></ul></ul></div>
</body></html>

输出如下:

代码示例2:

在下面的代码中, 可选参数传递给该方法。

<html><head><style>.main * {display: block;border: 2px solid lightgrey;color: grey;padding: 5px;margin: 15px;}</style><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><script>$(document).ready(function() {<!--Here among dom id first ancestor will select -->var item = document.getElementById("dom");$("li").closest("ul", item).css({"color": "green","border": "2px solid green"});});</script>
</head><body class="main">This is great-great-grandparent !<div style="width:500px;">div (great-grandparent)<ul id="dom">This is second ancestor !<ul id="dom">This is first ancestor !<li>This is direct parent !<span>This is span the child one !</span></li></ul></ul></div>
</body></html>

输出如下:

更多前端开发相关内容请参考:lsbin - IT开发技术:https://www.lsbin.com/

查看以下更多jQuery相关的内容:

  • jQuery GMaps地图插件:https://www.lsbin.com/3330.html
  • jQuery检查元素是否隐藏:https://www.lsbin.com/3310.html
  • jQuery error()方法:https://www.lsbin.com/3151.html

jQuery如何使用most()方法?代码示例和解析相关推荐

  1. doc python 颜色_Python wordcloud.ImageColorGenerator方法代码示例

    本文整理汇总了Python中wordcloud.ImageColorGenerator方法的典型用法代码示例.如果您正苦于以下问题:Python wordcloud.ImageColorGenerat ...

  2. java kryo_Kryo框架使用方法代码示例

    Kryo框架的source已移至https://github.com/EsotericSoftware/kryo ,进入此页面,然后点击右边的Download Zip按钮,就能下载到最新版本的Kryo ...

  3. python3 console input_Python console.colorize方法代码示例

    本文整理汇总了Python中pygments.console.colorize方法的典型用法代码示例.如果您正苦于以下问题:Python console.colorize方法的具体用法?Python ...

  4. python dateformatter_Python dates.DateFormatter方法代码示例

    本文整理汇总了Python中matplotlib.dates.DateFormatter方法的典型用法代码示例.如果您正苦于以下问题:Python dates.DateFormatter方法的具体用法 ...

  5. python中的scaler_Python preprocessing.MaxAbsScaler方法代码示例

    本文整理汇总了Python中sklearn.preprocessing.MaxAbsScaler方法的典型用法代码示例.如果您正苦于以下问题:Python preprocessing.MaxAbsSc ...

  6. python paperclip_Python pyplot.sca方法代码示例

    本文整理汇总了Python中matplotlib.pyplot.sca方法的典型用法代码示例.如果您正苦于以下问题:Python pyplot.sca方法的具体用法?Python pyplot.sca ...

  7. python fonttool_Python wx.Font方法代码示例

    本文整理汇总了Python中wx.Font方法的典型用法代码示例.如果您正苦于以下问题:Python wx.Font方法的具体用法?Python wx.Font怎么用?Python wx.Font使用 ...

  8. Java IOUtils.copy方法代码示例(亲测)

    本文整理汇总了Java中org.apache.commons.io.IOUtils.copy方法的典型用法代码示例.如果您正苦于以下问题:Java IOUtils.copy方法的具体用法?Java I ...

  9. python res_Python models.resnet152方法代码示例

    本文整理汇总了Python中torchvision.models.resnet152方法的典型用法代码示例.如果您正苦于以下问题:Python models.resnet152方法的具体用法?Pyth ...

  10. python asyncio future_Python asyncio.isfuture方法代码示例

    本文整理汇总了Python中asyncio.isfuture方法的典型用法代码示例.如果您正苦于以下问题:Python asyncio.isfuture方法的具体用法?Python asyncio.i ...

最新文章

  1. 在Ubuntu 16.04.6 LTS升级python 3.5.x到3.7.3之后导致gnome-ternimal无法打开的解决办法
  2. springmvc请求参数异常处理
  3. python智能办公系统_用 Python 自动化办公能做到哪些有趣或有用的事情?
  4. boost::foreach模块右值const的测试程序
  5. espflashdownloadtool连接串口失败_关于串口下载问题和超时
  6. 牛客多校2 - Cover the Tree(dfs序)
  7. Bioconda软件安装神器:多版本并存、环境复制、环境导出
  8. python如何定位路径_selenium_webdriver(python)查看文件路径,鼠标定位
  9. python3爬虫的简单使用-附百度贴吧等大量实例代码
  10. hdp对应hadoop的版本_hadoop不同版本区别
  11. 几款项目管理工具对比
  12. nodejs Log4js v2.x配置使用
  13. 【用Java模拟KTV点歌系统】
  14. Android 更换皮肤
  15. 公章,加盖公章,英语怎么说?
  16. C++ LinuxWebServer 2万7千字的面经长文(上)
  17. 高通平台wifi命令操作
  18. 夜深人静写算法(三十二)- 费马小定理
  19. 电脑回收站里不小心清理删除的文件怎么恢复?
  20. 一个(基于xfs协议)身份证扫描仪的理解

热门文章

  1. 禁用deepin的搜狗输入法简繁切换快捷键
  2. java 实现搜索附近人功能
  3. 什么是蜘蛛池?有什么作用?
  4. caj 服务器正在运行中,紧急求助:caj阅读器打不开
  5. 利用python计算n阶行列式的源代码
  6. blockly -- 颜色(Block colour)
  7. 编程之美学习笔记——数字相关(一)
  8. Vue3 Vite 项目踩坑札记
  9. 网易邮箱大师代收gmail
  10. Android Gmail签名客制化