简体   繁体   中英

Append an HTML tag, including an `onclick` attribute, to a page dynamically with jQuery

I'm working on a PhoneGap/Cordova application. I'm trying to dynamically append HTML to a container. The appended HTML has an anchor tag that includes an inline onclick event for the links to open on a click on iOS and Android, however I keep getting the error in the console

"Uncaught SyntaxError: missing ) after argument list"

Here's my code:

for (var i = 0; i < localData.length; i++) {
    $('.best-deals-page #deals-container').append('<a href="#" onclick="window.open('  + localData[i].deepLink + ' +  '_system'  ); "> ' + '</a>');
}

You need to escape single quotes in your window.open():

for( var i=0;  i < localData.length;  i++) {
    $('.best-deals-page #deals-container').append('<a href="#"   onclick="window.open(\'  + localData[i].deepLink + \' +  \'_system\'  ); "> </a>');
}

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