未捕获TypeError:Object#< HTMLDivElement>没有方法“addPanel” [英] Uncaught TypeError: Object #<HTMLDivElement> has no method 'addPanel'

查看:123
本文介绍了未捕获TypeError:Object#< HTMLDivElement>没有方法“addPanel”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图实现一个几乎跨浏览器的书签功能,并在SO上找到:
添加到收藏夹按钮

I am trying to implement an almost cross-browser bookmark functionality and found this on SO: Add to favorites button

现在,我使用@ PHPst的回答..

Now, I am using @PHPst's answer..

<script type="text/javascript">
$(function() {
    $("#bookmarkme").click(function() {
        if (window.sidebar) { // Mozilla Firefox Bookmark
            window.sidebar.addPanel(location.href,document.title,"");
        } else if( /*@cc_on!@*/false) { // IE Favorite
            window.external.AddFavorite(location.href,document.title); 
        } else if(window.opera && window.print) { // Opera Hotlist
            this.title=document.title;
            return true;
        } else { // webkit - safari/chrome
            alert('Press ' + (navigator.userAgent.toLowerCase().indexOf('mac') != - 1 ? 'Command/Cmd' : 'CTRL') + ' + D to bookmark this page.');
        }
    });
});
</script>

它在纯网页上工作,如下所示:
http://jsfiddle.net/GXas4/

it works on a plain webpage.. as demonstrated here: http://jsfiddle.net/GXas4/

但是当我使用它在一个wordpress模板,在chrome我得到一个js错误像这样:

but when i use it inside a wordpress template, in chrome i get a js error like this:

Uncaught TypeError: Object #<HTMLDivElement> has no method 'addPanel' 

它不会在firefox的控制台上返回错误,任何东西。

it does not return an error on the console of firefox but does not do anything too.

很多帖子在SO有这样的问题('未捕获TypeError:对象#没有方法'),但没有什么似乎指向我的权利

a lot of posts on SO have questions starting like this ('Uncaught TypeError: Object # has no method') but nothing seems to point me to the right direction.

有人知道为什么会发生这种情况吗?

Has anyone have an idea why this is happening?

推荐答案

p>正在使用的WordPress主题可能包含id = sidebar的元素。

It's possible that the WordPress theme you are using has an element with id=sidebar.

除非已经显式定义了具有相同名称的全局变量,否则全局变量将为每个具有id的元素创建。

Unless a global variable with the same name has been explicitly defined, a global variable will be created for each element that has an id.

因此,第一个测试是不可靠的。例如,即使在Chrome中,在stackoverflow页面上评估 window.sidebar 也是真的,因为网站使用的是具有这种id的元素。

So, the first test is unreliable. For example, evaluating window.sidebar on the stackoverflow page will be true even in Chrome, because the website uses an element with such an id.

这篇关于未捕获TypeError:Object#&lt; HTMLDivElement&gt;没有方法“addPanel”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆