Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript e、 防止默认在firefox中不工作_Javascript_Jquery - Fatal编程技术网

Javascript e、 防止默认在firefox中不工作

Javascript e、 防止默认在firefox中不工作,javascript,jquery,Javascript,Jquery,我是新来的,目前有一个问题,下面的代码不能在Firefox中工作 我试图通过使用e.preventDefault()来防止锚点默认。我还尝试了返回false,但它似乎仍然默认锚定位置 有什么想法吗?这是我的密码: <script type="text/javascript"> ... // tabs $('ul.tabNav').each(function(){ var $active, $content, $links = $(this).find('a

我是新来的,目前有一个问题,下面的代码不能在Firefox中工作

我试图通过使用
e.preventDefault()
来防止锚点默认。我还尝试了
返回false
,但它似乎仍然默认锚定位置

有什么想法吗?这是我的密码:

<script type="text/javascript">
...
    // tabs  
    $('ul.tabNav').each(function(){
    var $active, $content, $links = $(this).find('a');
    $active = $links.first().addClass('active');
    $content = $($active.attr('href'));
    $links.not(':first').each(function () {
        $($(this).attr('href')).hide();
    });

    $(this).on('click', 'a', function(e){
        // Make the old tab inactive.
        $active.removeClass('active');
        $content.hide();

        // Update the variables with the new link and content
        $active = $(this);
        $content = $($(this).attr('href'));

        // Make the tab active.
        $active.addClass('active');
        $content.show();    

        // Prevent the anchor's default click action
    e.preventDefault(); 
    });
});
});
</script>

...
//标签
$('ul.tabNav')。每个(函数(){
var$active,$content,$links=$(this.find('a');
$active=$links.first().addClass('active');
$content=$($active.attr('href'));
$links.not(“:first”)。每个(函数(){
$($(this.attr('href')).hide();
});
$(此)。在('click','a',函数(e)上{
//使旧选项卡处于非活动状态。
$active.removeClass('active');
$content.hide();
//使用新链接和内容更新变量
$active=$(此项);
$content=$($(this.attr('href'));
//使选项卡处于活动状态。
$active.addClass('active');
$content.show();
//防止锚定的默认单击操作
e、 预防默认值();
});
});
});

我会把
e.preventDefault()首先,因此无论发生什么都会调用它

在没有看到您的标记的情况下,我不能确定,但我猜是
$($(this.attr('href'))
正在抛出一个错误,该错误会停止执行,因此永远不会到达
e.preventDefault()

$(this).在('click','a',函数(e)
{
//防止锚定的默认单击操作
e、 预防默认值();
//使旧选项卡处于非活动状态。
$active.removeClass('active');
$content.hide();
//使用新链接和内容更新变量
$active=$(此项);

$content=$($(this.attr('href'));//你能发布一个演示来重现这个问题吗?默认情况下,你指的当然是href属性中的任何内容,而不是像UI选项卡这样的插件添加的内容?你似乎在使用jQuery。
e.preventDefault()
肯定“有效”。请提供更多信息。请查看控制台以了解错误。在jQuery中包装href属性通常不会导致错误。@adeneo:当然会。无效的选择器将引发错误。@adeneo
$(“/foo”)
可以。@adeneo:我认为这是最有可能的候选人。我在代码中没有看到任何其他可能抛出的东西,OP说
返回false;
也不起作用,所以我们知道(并且可以假设)这与
.preventDefault
@BlueSkies无关-因为问题的顶部有标签代码,我宁愿假设问题是OP试图在标签功能上防止默认,这当然不起作用,即使返回false也不行,但由于这只是一个假设,我不会将其作为答案发布.
$(this).on('click', 'a', function (e)
{
    // Prevent the anchor's default click action
    e.preventDefault(); 

    // Make the old tab inactive.
    $active.removeClass('active');
    $content.hide();

    // Update the variables with the new link and content
    $active = $(this);
    $content = $($(this).attr('href')); // <-- is probably erroring here.

    // Make the tab active.
    $active.addClass('active');
    $content.show();    
});