【问题标题】:insert rel attribute with javascript用javascript插入rel属性
【发布时间】:2013-06-21 18:13:08
【问题描述】:

我有这个功能:

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

【问题讨论】:

    标签: javascript jquery rel


    【解决方案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');
    }
    

    【讨论】:

    • 谢谢,但我也对 javascript 版本感兴趣,因为其余代码主要是用 javascript 编写的
    • @Alecs:然后使用document.createElement
    • 我试过了还是不行,可能是因为 .appendTo 所以可以在我发布的代码中插入 rel 吗?
    • @Alecs:你这样做的方式不会很好。
    • 我已将 appendTo 设置为容器 div
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多