简体   繁体   中英

Add HTML elements into an object

I want to add HTML elements in form of variable into an object by using JQuery *or without.

HTML elements

<a href="#" class="edit">EDIT</a>

Now I am just using this elements as variable...

var link = "<a href="+'"'+"#"+ '"'+ " class="+'"'+"edit"+'"'+">EDIT</a>";

Obj.addvariable(link) ?????????  // This Object could be any ID Or Class Or Div 

jQuery

$('body').append(link);

JavaScript

var element = document.createElement('a');
element.href = 'a';
element.className = 'b'
element.innerHTML = 'EDIT';
document.body.appendChild(element);

我不太了解您的问题,但也许jquery数据正是您要寻找的

you can add values to any properties even if they don't exist. javascript automatically creates them for you

Obj.anyProperty = 'new value';

if you want to append an element to a html element you can either use append:

$(element).append(link);

or appendTo:

$(link).appendTo(element)

You can use this in jquery as

$('#elementId').append(link);   // with id
$('.elementClass').append(link);   // with class, append to all elements of this class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM