使用javascript插入rel属性

时间:2013-06-21 18:13:09

标签: javascript jquery rel

我有这个功能:

 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属性添加到标记中。关于如何使这项工作的任何建议?

1 个答案:

答案 0 :(得分:1)

由于您已经在使用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');
}