使用javascript插入rel属性

insert rel attribute with javascript

本文关键字:属性 rel 插入 javascript 使用      更新时间:2023-09-26

我有这个函数:

 if (location.locationUrl != '') {
content += "<a class='viewLocationPage btn corePrettyStyle'  " +   
(mapObject.options.openinnew == false ? "" : "target='_blank'") +  
" href='" + location.locationUrl + "' >View location detail</a>";
        }

我添加了rel属性,如下所示:

if (location.locationUrl != '') {
            content += "<a class='viewLocationPage btn corePrettyStyle'  " + 
            (mapObject.options.openinnew == false ? "" : "target='_blank'") +
            "rel='prettyPhoto[iframes]'" + " href='" + location.locationUrl + "' >View location detail</a>";
        }

但不起作用,它没有在标记中添加任何rel属性。关于如何使这项工作发挥作用,有什么建议吗?

由于您已经在使用jQuery,因此使用jQuery构造函数创建元素会更容易:

if (location.locationUrl != '') {
    $('<a>', {
        'class': 'viewLocationPage btn corePrettyStyle',
        target: mapObject.options.openinnew ? '_blank' : '',
        rel: 'prettyPhoto[iframes]',
        href: location.locationUrl,
        text: 'View location detail'
    }).appendTo('#selector');
}