Javascript getElementById 返回字符串

发布于 2024-12-24 21:30:30 字数 399 浏览 5 评论 0原文

我遇到了 getElementById 的问题,这让我很沮丧。我期望该函数返回对对象的引用,并出于调试目的在控制台中显示该对象。

问题是它没有,它返回一个字符串,一个烦人的无用字符串。经过几个小时的尝试解决这个问题后,我有可能错过了一些明显的东西,但有人可以指出我正确的方向吗?

如果直接放入 HTML 文件中,下面的代码可以工作,但如果在函数中使用,则不会工作,并且在 JSFiddle 上测试它时也不起作用。如果有人能启发我为什么它在 JSFiddle 上不起作用,我希望这能让我深入了解如何在插件中解决这个问题。

JSFiddle : http://jsfiddle.net/dYtxq/

非常感谢。

I'm experiencing a problem with getElementById and it's driving me doolaly. I'm expecting the function to return the reference to the object and for the purposes of debugging show the object in the console.

The problem is it doesn't, it returns a string, a annoying useless string. There's a chance that after hours of trying to work this out I'm missing something glaring obvious, but could someone point me in the right direction please.

The code below works if just placed directly into the HTML file, but won't if used within a function and doesn't work when testing it on JSFiddle. If someone could enlighten me why it doesn't work on JSFiddle I'm hoping that'll give me in the insight to work it out in the plugin.

JSFiddle : http://jsfiddle.net/dYtxq/

Many thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉风有信 2024-12-31 21:30:30

document.getElementById() 不返回字符串,而是一个对象。

console.log() 正在写入元素的字符串表示形式。如果您想查看 DOM 对象,可以使用console.dir()。

document.getElementById() is not returning a string, but an object.

console.log() is writing the string representation of the element. If you want to see the DOM object you can use console.dir().

梦情居士 2024-12-31 21:30:30

它工作得很好,例如尝试在你的小提琴中执行此操作:

byId('timebar').innerHTML = "Test";

将其添加到你的小提琴版本中: http: //jsfiddle.net/dYtxq/5/

It is working just fine, try doing this in you fiddle for instance:

byId('timebar').innerHTML = "Test";

Added it to a version of your fiddle: http://jsfiddle.net/dYtxq/5/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文