abi*_*964 3 javascript jquery dom
如何使用jquery创建类似于div之类的东西?最好使用类似于$(document.createElement('div'));或更快的东西$("<div spry:region="myDs">{hostname}</div>");
<div spry:region="myDs">
{hostname}
</div>
Run Code Online (Sandbox Code Playgroud)
编辑
如何将此代码放在另一个JS文件中并使用它,因为"<%= request.getParameter(\"filepath\") %>"它产生了问题
var cpath="<%= request.getParameter(\"filepath\") %>";
var myDs = new Spry.Data.XMLDataSet(cpath, "csmclient/system");
$(document).ready(function(){
$('<div>') // Creates the element
.attr('spry:region','myDs') // Sets the attribute spry:region="myDs"
.html('{hostname}') // Sets the inner HTML to {hostname}
.appendTo('body');
});
Run Code Online (Sandbox Code Playgroud)
Tom*_*han 11
我很惊讶没有人完全利用jQuery的链接功能:
$('<div>') // Creates the element
.attr('spry:region','myDs') // Sets the attribute spry:region="myDs"
.html('{hostname}') // Sets the inner HTML to {hostname}
.appendTo('body') // Append the newly created element to body
Run Code Online (Sandbox Code Playgroud)
这是一个jFiddle示例,显示了它的实际效果.
更新以响应您的编辑
您将无法Request在外部javascript文件中使用该对象,因为外部脚本文件是由浏览器在单独的请求中提取的.您可以通过编写返回javascript文件的ASP.NET Handler脚本轻松解决问题,方法如下:
yourscriptname.js.ashx.Response.Write()调用或其他内容...src将<script>元素的标记更改somefile.js为somefile.js.asp?filepath=your/file.path.这样,当浏览器发送javascript请求时,请求对象具有值,"filepath"并且一切都将再次起作用.当然,your/file.path在首先渲染它时,您需要更改为更相关的内容,以便从请求参数中获取值到页面.