开发者

create Html elements using jquery

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>");

<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

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');
});


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

$('<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 example that shows it in action.

UPDATE in response to your edit
You won't be able to use the Request object 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:

  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 src tag of your <script> element from somefile.js to somefile.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.path to something more relevant when you render it in the first place, in order to get the value from the request parameters to the page.


To create elemts in jQuery and append them:

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

look here for full reference


First of all, document.createElement('div') is javascript, not jQuery. If you don't want to use 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 :) .


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

$('<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")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜