-2

我有一个javascript代码:

javascript: function iptxt() {
    var d = document;
    try {
        if (!d.body) throw (0);
        window.location = 'http://www.instapaper.com/text?u=' + encodeURIComponent(d.location.href);
    } catch (e) {
        alert('Please wait until the page has loaded.');
    }
}
iptxt();
void(0)

我会这样使用它:

< href="javascript:function iptxt(){var d=document;try{if(!d.body)throw(0);window.location='http://www.instapaper.com/text?u='+encodeURIComponent(d.location.href);}catch(e){alert('Please wait until the page has loaded.');}}iptxt();void(0)">Go</a>

但我想将此代码与 jquery 一起使用,如下所示:

<a href="#" id="go">Go</a>

以及将与 id go 集成的 jquery 代码是什么。

谢谢。

4

3 回答 3

3

您只需将函数调用放在给click的 callack 中:

<script>
$(function(){
    function iptxt(){
       var d=document;
       try{
         if(!d.body)throw(0);window.location='http://www.instapaper.com/text?u='+encodeURIComponent(d.location.href);
       } catch(e){alert('Please wait until the page has loaded.');}
    }

    $('#go').click(iptxt);
});
</script>

但这是最基本的 jQuery 任务。请看一下jQuery 教程

于 2013-01-11T12:03:29.463 回答
1

试试这个

$("#go").click(function(){
    var d = document;
    try{
        if(!d.body){
            window.location = 'http://www.instapaper.com/text?u=' + encodeURIComponent(d.location.href);
        }
    }
    catch(e){
        alert('Please wait until the page has loaded.')
    }
})
于 2013-01-11T12:17:13.060 回答
0

我不确定你为什么这样做对if(!d.body)throw(0);我没有多大意义。但这是在 jQuery 中的做法。

$("#go").click(function(event) {
     event.preventDefault();
     var d=document;
     try{
          if(!d.body)throw(0);
          window.location='http://www.instapaper.com/text?u='+encodeURIComponent(d.location.href);
     }catch(e){
          alert('Please wait until the page has loaded.');
     }
});
于 2013-01-11T12:05:27.323 回答