向javascript代码中添加一个href类

adding a href class to javascript code?

本文关键字:一个 href javascript 代码 添加      更新时间:2023-09-26

我使用一个jquery片段来拉入wordpress rss提要到我的网站。我是编码新手,很幸运地完成了这个程序。

我的问题是我怎么能传递一个href class="iframe"的代码,看起来像这样?

rssoutput+="<li><a href='" + thefeeds[i].link + "'>" + thefeeds[i].title + "</a></li>"

谢谢!

<script>
$(document).ready(function(){
        //Examples of how to assign the ColorBox event to elements
        $(".group1").colorbox({rel:'group1'});
        $(".group2").colorbox({rel:'group2', transition:"fade"});
        $(".group3").colorbox({rel:'group3', transition:"none", width:"75%", height:"75%"});
        $(".group4").colorbox({rel:'group4', slideshow:true});
        $(".ajax").colorbox();
        $(".youtube").colorbox({iframe:true, innerWidth:425, innerHeight:344});
        $(".iframe").colorbox({iframe:true, width:"80%", height:"100%"});
        $(".inline").colorbox({inline:true, width:"50%"});
        $(".callbacks").colorbox({
            onOpen:function(){ alert('onOpen: colorbox is about to open'); },
            onLoad:function(){ alert('onLoad: colorbox has started to load the targeted content'); },
            onComplete:function(){ alert('onComplete: colorbox has displayed the loaded content'); },
            onCleanup:function(){ alert('onCleanup: colorbox has begun the close process'); },
            onClosed:function(){ alert('onClosed: colorbox has completely closed'); }
        });
        //Example of preserving a JavaScript event for inline calls.
        $("#click").click(function(){ 
            $('#click').css({"background-color":"#fff", "color":"#fff", "cursor":"inherit"}).text("Open this window again and this message will still be here.");
            return false;
        });
    });
</script>

只需将class属性添加到字符串

rssoutput+="<li><a href='" + thefeeds[i].link + "' class='iframe'>" + thefeeds[i].title + "</a></li>"

编辑

如果iframe的ID是myFrame
rssoutput += "<li><a href='" + thefeeds[i].link + "' class='iframe' target='myFrame'>" + thefeeds[i].title + "</a></li>"

学习编写较少的"字符串"代码(所有代码都是通过字符串操作完成的),而是使用结构化对象。如果你使用jQuery,你也可以使用github.com上的jQuery -mochikit-tags项目(发现它留给读者作为练习:-)),以编程方式创建html,并使用jQuery结构,如appendappendTo,如下所示:

$.LI(
  $.A(
    {'href': thefeeds[i].link, 'class': 'iframe'},
    thefeeds[i].title
  )
).appendTo('#containerOfRssOutput');