大佬教程收集整理的这篇文章主要介绍了jQuery:在DOM之外选择和操作html元素,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我的问题是,是否有任何方法来实例化一个外部的html块,然后在将其插入DOM之前对其进行操作。
script_1.js:
$(document).ready(function () { $("#TESTButton").click(function () { $("#externalPageDiv").html("Hello world"); }); });
外部页面html:
<head> <Meta name="viewport" content="width=device-width,initial-scale=1"> <Meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0-rc.1/jquery.mobile-1.1.0-rc.1.min.css" /> <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.1.0-rc.1/jquery.mobile-1.1.0-rc.1.min.js"></script> <script src="script_1.js"></script> </head> <body> <div data-role="page" id="externalPage" data-add-BACk-btn="true"> <div data-role="content" id="externalPageContent"> <div id="externalPageDiv"></div>external page</div> </div> </body>
主页Html:
<head> <Meta name="viewport" content="width=device-width,initial-scale=1"> <Meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0-rc.1/jquery.mobile-1.1.0-rc.1.min.css" /> <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.1.0-rc.1/jquery.mobile-1.1.0-rc.1.min.js"></script> <script src="script_1.js"></script> </head> <body> <div data-role="page" id="internalPage_1" data-add-BACk-btn="true"> <div data-role="content" id="internalPageContent_1"> <a href="externalPage.html" id="TESTButton" data-role="button" rel="external">Page Change</a> </div> </div> <div data-role="page" id="mainPage" data-add-BACk-btn="true"> <div data-role="content" id="mainPageContent">Main Page</div> </div> </body>
尝试这个:
var codeToProcess = "<div>" + " <div id='myDiv1'>a</div>" + " <div id='myDiv2'>b</div>" + " <div id='myDiv3'>c</div>" + "</div>"; var $toProcess = $( codeToProcess ); $toProcess.find( "div" ).addClass( "processed" ); // getTing by id before insertion alert( $toProcess.find( "#myDiv1" ).html() ); alert( $( "#myDiv1" ).html() ); // this will return null,since the divs // were not yet added to the document $toProcess.appendTo( "#container" ); // or $( "#container" ).html( $toProcess ); // getTing by id after insertion alert( $( "#myDiv2" ).html() );
jsfiddle:http://jsfiddle.net/davidbuzatto/me7T3/
编辑:
要从外部文件插入代码,可以使用加载功能。您可以在这里看到一个例子:http://jsfiddle.net/davidbuzatto/zuFsc/请注意,在本示例中,我使用echo服务os jsfiddle模拟外部文件。看看这里How do you output a view file as an ajax response in Java?和这里http://api.jquery.com/load/
以上是大佬教程为你收集整理的jQuery:在DOM之外选择和操作html元素全部内容,希望文章能够帮你解决jQuery:在DOM之外选择和操作html元素所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。