Javascript 使用 jquery 创建 Html 元素

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

create Html elements using jquery

javascriptjquerydom

提问by AabinGunz

how can I create something like below div using jquery? preferably using something similar to $(document.createElement('div'));or something faster apart from $("<div spry:region="myDs">{hostname}</div>");

如何使用 jquery 创建类似下面的 div 的东西?最好使用类似于$(document.createElement('div'));或更快的东西$("<div spry:region="myDs">{hostname}</div>");

<div spry:region="myDs">
    {hostname}
</div>

Edited

已编辑

How to put this code in another JS file and use it, coz "<%= request.getParameter(\"filepath\") %>"is creating problem

如何将此代码放在另一个 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');
});

回答by Tomas Aschan

I'm surprised no one has yet fully made use of jQuery's chaining capabilities:

我很惊讶还没有人充分利用 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

Here's a jFiddle examplethat shows it in action.

这是一个jFiddle 示例,显示了它的实际效果。

UPDATEin response to your edit
You won't be able to use the Requestobject in an external javascript file like that, since the external script file is fetched by the browser in a separate request. You could easily solve the problem by writing an ASP.NET Handler script that returns your javascript file, by doing the following:

更新以响应您的编辑
您将无法Request在这样的外部 javascript 文件中使用该对象,因为浏览器在单独的请求中获取了外部脚本文件。您可以通过编写返回 javascript 文件的 ASP.NET 处理程序脚本来轻松解决该问题,方法如下:

  1. Create a new handler in your ASP.NET project. Name it something sensible, like yourscriptname.js.ashx.
  2. Copy all your javascript code into the right place in the handler (you probably want a Response.Write()call or something...
  3. Change the srctag of your <script>element from somefile.jsto somefile.js.asp?filepath=your/file.path.
  1. 在您的 ASP.NET 项目中创建一个新的处理程序。将其命名为合理的名称,例如yourscriptname.js.ashx.
  2. 将您所有的 javascript 代码复制到处理程序中的正确位置(您可能想要一个Response.Write()电话或其他东西......
  3. src<script>元素的标签从更改somefile.jssomefile.js.asp?filepath=your/file.path

This way, when the request for the javascript is sent by the browser, the request object has a value for "filepath"and everything will work again. Of course, you'll want to change your/file.pathto something more relevant when you render it in the first place, in order to get the value from the request parameters to the page.

这样,当浏览器发送对 javascript 的请求时,请求对象有一个值"filepath",一切都会再次工作。当然,your/file.path当您首先呈现它时,您会希望更改为更相关的内容,以便从请求参数中获取值到页面。

回答by Nicola Peluchetti

To create elemts in jQuery and append them:

要在 jQuery 中创建元素并附加它们:

jQuery('<div/>', {
    "spry:region": "myDS",
    text: '{hostname}'
}).appendTo('body');

look herefor full reference

这里完全参考

回答by Vithozor

First of all, document.createElement('div')is javascript, not jQuery. If you don't want to use jQuery:

首先,document.createElement('div')是javascript,而不是jQuery。如果您不想使用 jQuery:

var main = document.getElementById('main');//where you want the div to be added
var div = document.createElement('div');
div.setAttribute("spry:region","myDs");
div.innerHTML = "{hostname}";
main.appendChild(div);

But jQuery makes easier to work with the DOM, so you can use one of the above answers :) .

但是 jQuery 可以更轻松地使用 DOM,因此您可以使用上述答案之一:)。

回答by jargalan

$("html tags").appendTo("where you want to add")

$("html 标签").appendTo("要添加的地方")

$('<div spry:region="myDs">{hostname}</div>').appendTo("body")
$('<div spry:region="myDs">{hostname}</div>').appendTo("#content")

or

或者

var div = $(document.createElement('div'));
div.html("{hostname}").attr('spry:region=', "myDs").appendTo("body")