简体   繁体   中英

How do I create a element as a jQuery object?

Now i create a jQuery object by using

var content = $("Some string" + <a target="_blank" href="http://www.mypage.com"> + </a>);

But this seems not working.

How to fixed it?

The jQuery constructor will only create you an element. It won't display it on the document until you append it. Also, the elements must be parsed as a string. Example:

 var content = $('<span>Some string</span><a target="_blank" href="http://www.mypage.com">Link</a>'); content.appendTo('body'); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

In your code, the HTML wasn't a string, so it most likely showed a SyntaxError or a TypeError.

Try substituting a string representation of content for wrapping in jQuery()

 var content = "Some string" + "<a target=_blank href=http://www.mypage.com> + </a>"; $("body").append(content); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

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