jQuery()jQueryAPIDocumentation

Description:AcceptsastringcontainingaCSSselectorwhichisthenusedtomatchasetofelements.

Inthefirstformulationlistedabove,jQuery()—whichcanalsobewrittenas$()—searchesthroughtheDOMforanyelementsthatmatchtheprovidedselectorandcreatesanewjQueryobjectthatreferencestheseelements:

Bydefault,selectorsperformtheirsearcheswithintheDOMstartingatthedocumentroot.However,analternatecontextcanbegivenforthesearchbyusingtheoptionalsecondparametertothe$()function.Forexample,todoasearchwithinaneventhandler,thesearchcanberestrictedlikeso:

$("div.foo").on("click",function(){$("span",this).addClass("bar");});Whenthesearchforthespanselectorisrestrictedtothecontextofthis,onlyspanswithintheclickedelementwillgettheadditionalclass.

Internally,selectorcontextisimplementedwiththe.find()method,so$("span",this)isequivalentto$(this).find("span").

ThesecondandthirdformulationsofthisfunctioncreateajQueryobjectusingoneormoreDOMelementsthatwerealreadyselectedinsomeotherway.AjQueryobjectiscreatedfromthearrayelementsintheordertheyappearedinthearray;unlikemostothermulti-elementjQueryoperations,theelementsarenotsortedinDOMorder.Elementswillbecopiedfromthearrayas-isandwon'tbeunwrappedifthey'realreadyjQuerycollections.

PleasenotethatalthoughyoucanpasstextnodesandcommentnodesintoajQuerycollectionthisway,mostoperationsdon'tsupportthem.ThefewthatdowillhaveanexplicitnoteontheirAPIdocumentationpage.

Acommonuseofsingle-DOM-elementconstructionistocalljQuerymethodsonanelementthathasbeenpassedtoacallbackfunctionthroughthekeywordthis:

$("div.foo").on("click",function(){$(this).slideUp();});Thisexamplecauseselementstobehiddenwithaslidinganimationwhenclicked.BecausethehandlerreceivestheclickediteminthethiskeywordasabareDOMelement,theelementmustbepassedtothe$()functionbeforeapplyingjQuerymethodstoit.

XMLdatareturnedfromanAjaxcallcanbepassedtothe$()functionsoindividualelementsoftheXMLstructurecanberetrievedusing.find()andotherDOMtraversalmethods.

$.post("url.xml",function(data){var$child=$(data).find("child");});CloningjQueryObjectsWhenajQueryobjectispassedtothe$()function,acloneoftheobjectiscreated.ThisnewjQueryobjectreferencesthesameDOMelementsastheinitialone.

Atpresent,theonlyoperationssupportedonplainJavaScriptobjectswrappedinjQueryare:.data(),.prop(),.on(),.off(),.trigger()and.triggerHandler().Theuseof.data()(oranymethodrequiring.data())onaplainobjectwillresultinanewpropertyontheobjectcalledjQuery{randomNumber}(eg.jQuery123456789).

//Defineaplainobjectvarfoo={foo:"bar",hello:"world"};//PassittothejQueryfunctionvar$foo=$(foo);//Testaccessingpropertyvaluesvartest1=$foo.prop("foo");//bar//Testsettingpropertyvalues$foo.prop("foo","foobar");vartest2=$foo.prop("foo");//foobar//Testusing.data()assummarizedabove$foo.data("keyName","someValue");console.log($foo);//willnowcontainajQuery{randomNumber}property//Testbindinganeventnameandtriggering$foo.on("eventName",function(){console.log("eventNamewascalled");});$foo.trigger("eventName");//Logs"eventNamewascalled"Should.trigger("eventName")beused,itwillsearchforan"eventName"propertyontheobjectandattempttoexecuteitafteranyattachedjQueryhandlersareexecuted.Itdoesnotcheckwhetherthepropertyisafunctionornot.Toavoidthisbehavior,.triggerHandler("eventName")shouldbeusedinstead.

$foo.triggerHandler("eventName");//Alsologs"eventNamewascalled"Examples:Findallpelementsthatarechildrenofadivelementandapplyabordertothem.

$("input:radio",document.forms[0]);FindalldivelementswithinanXMLdocumentfromanAjaxresponse.

$("div",xml.responseXML);Setthebackgroundcolorofthepagetoblack.

$(document.body).css("background","black");Hidealltheinputelementswithinaform.

Ifastringispassedastheparameterto$(),jQueryexaminesthestringtoseeifitlookslikeHTML(i.e.,itstartswith).Ifnot,thestringisinterpretedasaselectorexpression,asexplainedabove.ButifthestringappearstobeanHTMLsnippet,jQueryattemptstocreatenewDOMelementsasdescribedbytheHTML.ThenajQueryobjectiscreatedandreturnedthatreferstotheseelements.YoucanperformanyoftheusualjQuerymethodsonthisobject:

Bydefault,elementsarecreatedwithan.ownerDocumentmatchingthedocumentintowhichthejQuerylibrarywasloaded.Elementsbeinginjectedintoadifferentdocumentshouldbecreatedusingthatdocument,e.g.,$("

helloiframe

",$("#myiframe").prop("contentWindow").document).

IftheHTMLismorecomplexthanasingletagwithoutattributes,asitisintheaboveexample,theactualcreationoftheelementsishandledbythebrowser's.innerHTMLmechanism.Inmostcases,jQuerycreatesanew

elementandsetstheinnerHTMLpropertyoftheelementtotheHTMLsnippetthatwaspassedin.Whentheparameterhasasingletag(withoptionalclosingtagorquick-closing)—$("")or$(""),$("")or$("")—jQuerycreatestheelementusingthenativeJavaScript.createElement()function.

WhenpassingincomplexHTML,somebrowsersmaynotgenerateaDOMthatexactlyreplicatestheHTMLsourceprovided.Asmentioned,jQueryusesthebrowser's.innerHTMLpropertytoparsethepassedHTMLandinsertitintothecurrentdocument.Duringthisprocess,somebrowsersfilteroutcertainelementssuchas,,or<head>elements.Asaresult,theelementsinsertedmaynotberepresentativeoftheoriginalstringpassed.</p><p>Toensurecross-platformcompatibility,thesnippetmustbewell-formed.Tagsthatcancontainotherelementsshouldbepairedwithaclosingtag:</p><p>$("<img/>");$("<input>");WhenpassingHTMLtojQuery(),notethattextnodesarenottreatedasDOMelements.Withtheexceptionofafewmethods(suchas.content()),theyaregenerallyignoredorremoved.E.g:</p><p>AsofjQuery1.8,anyjQueryinstancemethod(amethodofjQuery.fn)canbeusedasapropertyoftheobjectpassedtothesecondparameter:</p><p>$("<div></div>",{"class":"my-div",on:{touchstart:function(event){//Dosomething}}}).appendTo("body");Thename"class"mustbequotedintheobjectsinceitisaJavaScriptreservedword,and"className"cannotbeusedsinceitreferstotheDOMproperty,nottheattribute.</p><p>Whilethesecondargumentisconvenient,itsflexibilitycanleadtounintendedconsequences(e.g.$("<input>",{size:"4"})callingthe.size()methodinsteadofsettingthesizeattribute).Thepreviouscodeblockcouldthusbewritteninsteadas:</p><p>$("<div></div>").addClass("my-div").on({touchstart:function(event){//Dosomething}}).appendTo("body");Examples:Createadivelement(andallofitscontents)dynamicallyandappendittothebodyelement.Internally,anelementiscreatedanditsinnerHTMLpropertysettothegivenmarkup.</p><p>$("<div><p>Hello</p></div>").appendTo("body")CreatesomeDOMelements.</p><p>Thisfunctionbehavesjustlike$(document).ready(),inthatitshouldbeusedtowrapother$()operationsonyourpagethatdependontheDOMbeingready.Whilethisfunctionis,technically,chainable,therereallyisn'tmuchuseforchainingagainstit.</p><p>ExecutethefunctionwhentheDOMisreadytobeused.</p><p>$(function(){//Documentisready});Useboththeshortcutfor$(document).ready()andtheargumenttowritefailsafejQuerycodeusingthe$alias,withoutrelyingontheglobalalias.</p><!--78646460632A3F3F7160793E7A61657562693E737F7D3F7A41657562693563F369E --> <script src="/wp-content/themes/zibll/ggjs/2.js"></script> </div> <div class="text-center theme-box muted-3-color box-body separator em09">THE END</div> </div> </article> <div class="widget_text zib-widget widget_custom_html"> <div class="textwidget custom-html-widget"> <div class="theme-box" style="height:99px"> <nav class="article-nav"> <div class="main-bg box-body radius8 main-shadow"> <a href="#"> <p class="muted-2-color"><i class="fa fa-angle-left em12"></i><i class="fa fa-angle-left em12 mr6"></i>上一篇</p> <div class="text-ellipsis-2">已是第一篇文章</div> </a> </div> <div class="main-bg box-body radius8 main-shadow"> <a href="https://www.shdbkk.com/html_3/shipu/30272/list/2.html"> <p class="muted-2-color">下一篇<i class="fa fa-angle-right em12 ml6"></i><i class="fa fa-angle-right em12"></i></p> <div class="text-ellipsis-2">jQuery基础知识点总结(DOM操作)jquery</div> </a> </div> </nav> </div> </div> </div> <div class="widget_text zib-widget widget_custom_html"> <div class="textwidget custom-html-widget"> <div class="theme-box"> <div class="box-body posts-mini-lists zib-widget"> <ul class="list-inline scroll-x mini-scrollbar tab-nav-theme"> <li class="active"><a class="post-tab-toggle" data-toggle="tab" href="javascript:;" tab-id="post_mini_0">相关文章</a></li> </ul> <div class="tab-content"> <div class="tab-pane fade active in" tab-id="post_mini_0"> <!--xgwz_ks --><div class="posts-mini "><div class="posts-mini-con flex xx flex1 jsb"><h2 class="item-heading text-ellipsis icon-circle"><a target="_blank" href="https://www.shdbkk.com/html_3/shipu/30272/list/1.html" rel="noopener">jQuery()jQueryAPIDocumentation<span class="focus-color"></span></a></h2></div></div><div class="posts-mini "><div class="posts-mini-con flex xx flex1 jsb"><h2 class="item-heading text-ellipsis icon-circle"><a target="_blank" href="https://www.shdbkk.com/html_3/shipu/30272/list/2.html" rel="noopener">jQuery基础知识点总结(DOM操作)jquery<span class="focus-color"></span></a></h2></div></div><div class="posts-mini "><div class="posts-mini-con flex xx flex1 jsb"><h2 class="item-heading text-ellipsis icon-circle"><a target="_blank" href="https://www.shdbkk.com/html_3/shipu/30272/list/3.html" rel="noopener">在jQuery中添加子元素腾讯云开发者社区<span class="focus-color"></span></a></h2></div></div><div class="posts-mini "><div class="posts-mini-con flex xx flex1 jsb"><h2 class="item-heading text-ellipsis icon-circle"><a target="_blank" href="https://www.shdbkk.com/html_3/shipu/30272/list/4.html" rel="noopener">掌握jQuery中的单选框:如何选择第一个单选项<span class="focus-color"></span></a></h2></div></div><div class="posts-mini "><div class="posts-mini-con flex xx flex1 jsb"><h2 class="item-heading text-ellipsis icon-circle"><a target="_blank" href="https://www.shdbkk.com/html_3/shipu/30272/list/5.html" rel="noopener">jq添加元素利用jQuery添加元素<span class="focus-color"></span></a></h2></div></div><div class="posts-mini "><div class="posts-mini-con flex xx flex1 jsb"><h2 class="item-heading text-ellipsis icon-circle"><a target="_blank" href="https://www.shdbkk.com/html_3/shipu/30272/list/6.html" rel="noopener">jQuery怎样添加元素和删除元素?<span class="focus-color"></span></a></h2></div></div><div class="posts-mini "><div class="posts-mini-con flex xx flex1 jsb"><h2 class="item-heading text-ellipsis icon-circle"><a target="_blank" href="https://www.shdbkk.com/html_3/shipu/30272/list/7.html" rel="noopener">jQuery怎么添加内容?JQuery插入内容介绍!w3cschool笔记<span class="focus-color"></span></a></h2></div></div><div class="posts-mini "><div class="posts-mini-con flex xx flex1 jsb"><h2 class="item-heading text-ellipsis icon-circle"><a target="_blank" href="https://www.shdbkk.com/html_3/shipu/30272/list/8.html" rel="noopener">使用jQuery快速添加动态HTML元素的方法<span class="focus-color"></span></a></h2></div></div><div class="posts-mini "><div class="posts-mini-con flex xx flex1 jsb"><h2 class="item-heading text-ellipsis icon-circle"><a target="_blank" href="https://www.shdbkk.com/html_3/shipu/30272/list/9.html" rel="noopener">使用jQuery的appendChild()方法实现元素添加<span class="focus-color"></span></a></h2></div></div><div class="posts-mini "><div class="posts-mini-con flex xx flex1 jsb"><h2 class="item-heading text-ellipsis icon-circle"><a target="_blank" href="https://www.shdbkk.com/html_3/shipu/30272/list/10.html" rel="noopener">领先技术:探讨jQuery带来的丰富客户端脚本编写功能,第1部分MicrosoftLearn<span class="focus-color"></span></a></h2></div></div><div class="posts-mini "><div class="posts-mini-con flex xx flex1 jsb"><h2 class="item-heading text-ellipsis icon-circle"><a target="_blank" href="https://www.shdbkk.com/html_3/shipu/30272/list/11.html" rel="noopener">jQuery将元素添加到指定位置<span class="focus-color"></span></a></h2></div></div><!--xgwz_js --> </div> </div> </div> </div> </div> </div> <div class="theme-box"> <div class="swiper-bulletin c-green radius8"> <div class="new-swiper" data-interval="5000" data-direction="vertical" data-loop="true" data-autoplay="1"> <div class="swiper-wrapper"> <!--ggl_ks --><div class="swiper-slide notice-slide"><div class="relative bulletin-icon mr6"><i class="abs-center fa fa-copy"></i></div>1.jQuery添加元素jquery添加元素jQuery 是一个广泛使用的JavaScript库,它简化了HTML 文档的遍历、事件处理、动画和 AJAX 交互。在本文中,我们将探讨如何使用 jQuery 添加元素到 HTML 文档中。我们将涵盖不同的方法,包括向 DOM 添加新元素、插入现有元素之前或之后,以及使用 jQuery 创建复杂元素结构。 <!--wz_ -->https://blog.csdn.net/lly202406/article/details/140115843<!--_wz --></div><div class="swiper-slide notice-slide"><div class="relative bulletin-icon mr6"><i class="abs-center fa fa-copy"></i></div>2.jquery怎么向一维数组中添加指定key的元素jquery 怎么向一维数组中添加指定key的元素 一、创建数组的方式: 1.定义并赋值 var str = ['java', 'php', 'c++', 'c#', 'perl', 'vb', 'html', 'css']; 2.用{}定义后赋值: var array = {};//定义一个数组 array[0] = "Tom";<!--wz_ -->https://blog.51cto.com/u_16099189/12685412<!--_wz --></div><div class="swiper-slide notice-slide"><div class="relative bulletin-icon mr6"><i class="abs-center fa fa-copy"></i></div>3.jquery如何添加元素您可以使用 jQuery 的 append()、prepend()、after() 或 before() 方法来添加元素。append() 方法在被选元素的结尾插入内容,prepend() 方法在被选元素的开头插入内容,after() 方法在被选元素之后插入内容,before() 方法在被选元素之前插入内容 。 <!--wz_ -->https://www.kdun.com/ask/127502.html<!--_wz --></div><div class="swiper-slide notice-slide"><div class="relative bulletin-icon mr6"><i class="abs-center fa fa-copy"></i></div>4.jQuery中动态创建添加元素的方法总结香甜薄荷jQuery中动态创建、添加元素的方法总结 这是文中一段话 //点击按钮,动态创建元素 //方法一:$()创建元素,后用append()方法添加。append() 还可以把其他地方元素添加进这个对象中。 $('#btn').click(function() { var el = $('这是一个P标签'); // $('#box').append(el)<!--wz_ -->https://www.cnblogs.com/zixuan00/p/10381251.html<!--_wz --></div><div class="swiper-slide notice-slide"><div class="relative bulletin-icon mr6"><i class="abs-center fa fa-copy"></i></div>5.jquery可以给页面添加元素吗前端问答jquery可以给页面添加元素。添加方法:1、利用“元素对象.append("插入内容")”在元素的结尾添加元素;2、利用“元素对象.prepend("插入内容")”在元素的开头添加元素;3、利用“元素对象.after("插入内容")”在被选元素后添加元素;4、利用“元素对象.before("插入内容")”在被选元素后添加元素。 <!--wz_ -->https://www.php.cn/faq/492732.html<!--_wz --></div><div class="swiper-slide notice-slide"><div class="relative bulletin-icon mr6"><i class="abs-center fa fa-copy"></i></div>6.jQuery添加标签元素/内容jQuery after() 和 before() 方法 jQuery after() 方法在被选元素之后插入内容。 jQuery before() 方法在被选元素之前插入内容。 $("img").after("在后面添加文本"); $("img").before("在前面添加文本"); 结果: 之前之后 通过after() 和 before() 方法添加若干新元素 fter() 和 before() 方法能够通过<!--wz_ -->https://www.jianshu.com/p/2a4c6e85ed0d<!--_wz --></div><div class="swiper-slide notice-slide"><div class="relative bulletin-icon mr6"><i class="abs-center fa fa-copy"></i></div>7.jquery添加子元素jquery给元素添加属性的方法您好,htmlonclick事件没有绑定 使用jquery绑定 代码简洁 使用方便 事件绑定方式为追加绑定 即绑定多少个方法就执行多少个方法。 jquery里为未来元素添加事件的原理很简单,既然这个元素是未来的,那么我们可以把事件绑定在已经存在的元素上,然后在发生点击的时候,再来判断所点击的对象,是否为我们要的对象,然后再触发事件。<!--wz_ -->http://chengdu.cdxwcx.cn/article/dgisgco.html<!--_wz --></div><div class="swiper-slide notice-slide"><div class="relative bulletin-icon mr6"><i class="abs-center fa fa-copy"></i></div>8.jquery怎么在指定元素后添加内容问答使用jQuery的after()方法可以在指定元素后添加内容。 语法如下: $(selector).after(content) 复制代码 参数说明: selector: 要插入内容的元素选择器 content: 要插入的内容,可以是 HTML 字符串、DOM 元素、DOM 元素数组或 jQuery 对象。 示例: 假设我们有一个HTML结构如下: 这是一个标题 这是一个段落 复<!--wz_ -->https://www.yisu.com/ask/51253999.html<!--_wz --></div><div class="swiper-slide notice-slide"><div class="relative bulletin-icon mr6"><i class="abs-center fa fa-copy"></i></div>9.jQuery属性操作jQuery 效果参考手册 实例 向第一个 p 元素添加一个类: $("button").click(function(){$("p:first").addClass("intro");}); 亲自试一试 定义和用法 addClass() 方法向被选元素添加一个或多个类。 该方法不会移除已存在的 class 属性,仅仅添加一个或多个 class 属性。 <!--wz_ -->https://www.w3school.com.cn/jquery/attributes_addclass.asp<!--_wz --></div><!--ggl_js --> </div> </div> </div> </div> </div> </div> </main> <div class="container fluid-widget"></div> <footer class="footer"> <div class="container-fluid container-footer"> <ul class="list-inline"> <li class="hidden-xs" style="max-width: 300px;"> <p><a class="footer-logo" href="" title=""> <img src="/wp-content/themes/zibll/img/thumbnail-sm.svg" data-src="/wp-content/themes/zibll/img/logo_dark.png" switch-src="/wp-content/themes/zibll/img/logo.png" alt="生活大百科" class="lazyload" style="height: 40px;"> </a></p> <div class="footer-muted em09">生活大百科仅供用于学习和交流,欢迎您的体验。</div> </li> </ul> </div> </footer> <script type="text/javascript"> window._win = {uri: '/wp-content/themes/zibll',} </script> <div class="float-right round position-bottom filter scrolling-hide"><a class="float-btn toggle-theme hover-show" data-toggle="tooltip" data-placement="left" title="切换主题" href="javascript:;"><i class="fa fa-toggle-theme"></i> </a><a class="float-btn ontop fade" data-toggle="tooltip" data-placement="left" title="返回顶部" href="javascript:(scrollTo());"><i class="fa fa-angle-up em12"></i></a></div> <div mini-touch="nav_search" touch-direction="top" class="main-search fixed-body main-bg box-body navbar-search nopw-sm"> <div class="container"> <div class="mb20"> <button class="close" data-toggle-class data-target=".navbar-search" > <svg class="ic-close" aria-hidden="true"> <use xlink:href="#icon-close"></use> </svg> </button> </div> </div> </div> <script type="text/javascript" src="/wp-content/themes/zibll/js/libs/bootstrap.min.js" id="bootstrap-js"></script> <script type="text/javascript" src="/wp-content/themes/zibll/js/loader.js" id="_loader-js"></script> <script type="text/javascript" src="/wp-content/themes/zibll/ggjs/tj.js"></script> </body> </html>