0

我对 js 和 jquery 还很陌生,并且已经在一些代码上苦苦挣扎了一段时间。我看到发布了一些类似的问题,但是当我尝试实施解决方案时,我仍然没有得到我想要的行为。
推文已加载,有些有网址,有些则没有。如果用户单击推文中的 url,我希望它在新窗口中打开。我很确定 onclick 函数发生了什么事(可能没有正确分配给变量?)因为如果我删除 onclick 会为每个引入的 url 打开一个新窗口。
首先,我创建链接变量并拉出其余的文本,以便我可以将 url 作为超链接附加。如果用户单击 url,我想在新窗口中打开变量 link1(我直接从 api 中提取)。

           var link="";
           if(tweet.entities.urls[0]!=null){
            if(tweet.entities.urls[0].url!=null){
                var link1=tweet.entities.urls[0].url;
                link=replaceLinks(link1);
                $('link').live('click', function(){
                    window.open(link1);
                });
        }
        }
           if(tweet.geo==null){
                $("#geo").append('<p><img src="'+tweet.profile_image_url+'" widt="48" height="48" />'+tweet.text+link+'</p>');
          }

$('link').live('click', function(){行是我尝试的最后一种方法,尽管我尝试了许多其他 onclick 分配。我一直在 Firefox 中对此进行测试,并使用 aptana 以防万一。我会包含更多代码,但经过大量测试后,我真的认为问题出现在我所包含的行中,但如果这不足以确定问题,我很乐意包含更多代码。这是我的第一个网络应用程序,所以如果我做的事情很明显是错误的,请告诉我它是什么?!非常感谢!

4

1 回答 1

1

I am confused about what is the id of your link. You have quotes around your link variable, if it is supposed to contain the link id then it should be like this:

$(link).live('click', function(){
    window.open(link1);
});

If link is the actual id or a class of your link then it should be like this:

$('#link').live('click', function(){ // id
$('.link').live('click', function(){ // class

Edit:

Also to note, depending on the version of jQuery you are using, live() is deprecated and should be replace by on()

$(selector).on('click', function(){
    window.open(link1);
});
于 2012-08-04T17:31:00.603 回答