最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - create Html elements using jquery - Stack Overflow

programmeradmin3浏览0评论

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

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');
});
Share Improve this question edited Jun 16, 2011 at 8:44 Tomas Aschan 60.6k61 gold badges261 silver badges419 bronze badges asked Jun 15, 2011 at 13:01 AabinGunzAabinGunz 12.3k54 gold badges148 silver badges220 bronze badges 5
  • I doubt you can. jQuery is designed to work with HTML, not multi-namespace XML documents. – Quentin Commented Jun 15, 2011 at 13:03
  • @Quentin: I edited my question can you please take a look? thanks – AabinGunz Commented Jun 16, 2011 at 5:48
  • I took the liberty of rolling back your question to the latest edit that has an answer in this thread, since you were asking new questions in this one. If you can't wait untill tomorrow, when your question quota has room for some new ones, meta is the place to complain about it. – Tomas Aschan Commented Jun 16, 2011 at 8:49
  • Please don't change the meaning of questions after you've posted them, and at the very least, not after getting answers. One of the reasons behind the limit on number of questions per month is to avoid swamping the site with questions from people that don't have a long experience in their field, such as HTML and/or JavaScript. – Lasse V. Karlsen Commented Jun 16, 2011 at 11:58
  • @Lasse V.: I'll keep that in mind for next time. :) – AabinGunz Commented Jun 16, 2011 at 12:00
Add a comment  | 

4 Answers 4

Reset to default 12

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)

  1. 暂无评论