Javascript点击链接

时间:2013-02-19 12:24:03

标签: javascript jquery hyperlink click

我在jQuery.Script上有“tabslideout”:

$(function(){
    $('.slide-out-div').tabSlideOut({
        tabHandle: '.handle',                            
        pathToTabImage: '/images/contact.gif',       
        imageHeight: '122px',                              
        imageWidth: '40px',                                 
        tabLocation: 'left',                              
        speed: '300',                                       
        action: 'click',                                  
        topPos: '200px',                                   
        fixedPosition: true                               
    });
});

我想通过点击<a href="">Some Text</a>这样的链接打开此标签 我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

为链接指定ID,如下所示:

<a href="" id="some-id">Some Text</a>

然后使用此代码:

$(function() {
    $('#some-id').on('click', function(e) {
        e.preventDefault();
        $('.slide-out-div').tabSlideOut({
            tabHandle: '.handle',                            
            pathToTabImage: '/images/contact.gif',       
            imageHeight: '122px',                              
            imageWidth: '40px',                                 
            tabLocation: 'left',                              
            speed: '300',                                       
            action: 'click',                                  
            topPos: '200px',                                   
            fixedPosition: true                               
        });
    });
});

答案 1 :(得分:0)

要使代码更清晰,请将tabSlideout选项和滑动div存储在单独的变量中:

var tabOptions = {
    tabHandle: '.handle',                            
    pathToTabImage: '/images/contact.gif',       
    imageHeight: '122px',                              
    imageWidth: '40px',                                 
    tabLocation: 'left',                              
    speed: '300',                                       
    action: 'click',                                  
    topPos: '200px',                                   
    fixedPosition: true                               
};

var slider = $('.slide-out-div');

接下来,以某种方式识别您的链接。类通常比ID更好:

<a href="" class="tabLink">Some Text</a>

最后添加一个点击处理程序:

$('.tabLink').on('click', function(evnt) {
    evnt.preventDefault();
    slider.tabSlideOut(tabOptions);
});