Safari 原生代码 [英] Safari Native Code

查看:30
本文介绍了Safari 原生代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人熟悉 OS X Safari(版本 3 和 WebKit)中的原生代码吗?我正在使用 Javascript 来解析表单中的一些信息,我的一个输入名为标签".尝试使用以下方法获取该元素的值时:

Is anyone familiar with Native Code in OS X Safari (Version 3 and WebKit)? I'm using Javascript to parse some information in a form and one of my inputs is named "tags". When trying to get the value of that element using:

// button is being passed through a function as a DOM object
var tags = button.form.elements["tags"].value;

Safari 返回某种函数.我已经用它来提醒值,如function tags() { [native code] }"和节点树,但我不明白为什么我会遇到麻烦.如果有人有线索,请告诉我.我已经通过将输入的名称更改为其他名称并迭代所有元素并使用 if () 语句来确定它是否是我想要的元素来使其工作,但我非常好奇为什么 Apple 会限制使用任何名为标签"的表单元素...

Safari returns some kind of function. I've gotten it to alert values like "function tags() { [native code] }" and Node Trees but I just can't understand why I would be having trouble. If anyone has a clue, please let me know. I've gotten it to work by changing the name of the input to something else and also by iterating through all elements and using if () statements to determine whether it's the element I want, but I'm awfully curious as to why Apple would restrict the use of any form element named "tags"...

附言- 经过测试,在 Firefox 中运行良好.

P.S. - It's test and works fine in firefox.

推荐答案

[native code] 只是意味着它是一个内置于浏览器的函数,而不是用 JavaScript 编写的.tags 似乎是 DOM 的 WebKit 扩展,允许您通过标签名称获取表单中的元素列表.例如,如果我在 StackOverflow 页面上运行它,我会得到答案文本区域:

[native code] just means that it's a function that is built in to the browser, rather than written in JavaScript. tags appears to be a WebKit extension to the DOM to allow you to get a list of elements in the form by tag name. For instance, if I run this on the StackOverflow page, I get the answer text area:

document.getElementById('submit-button').form.elements.tags("textarea")[0]

问题是 JavaScript 中集合的索引也访问任何对象属性(包括方法),因此当您尝试访问命名元素 tags 时,您会得到 tags 上的方法WebKit 定义的 code>elements 对象.幸运的是,有一个解决方法;你可以调用 namedItem 在元素列表上通过 idname 获取项目:

The issue is that an index into a collection in JavaScript also access any object properties (including methods), so when you try to access your named element tags, you get instead the method on the elements object that WebKit defines. Luckily, there is a workaround; you can call namedItem on the elements list to get an item by id or name:

var tags = button.form.elements.namedItem("tags").value;

edit:请注意,即使在其他浏览器中,通常使用 namedItem 可能更好,以防您需要检索名为 itemlength 或类似的东西;否则,如果您将它们用作带有 [] 运算符的索引,您将获得内置方法 itemlength 而不是您的元素.

edit: Note that its probably better to use namedItem in general even in other browsers, in case you need to retrieve an element named item or length or something like that; otherwise, if you use them as an index with the [] operator, you'll get the built in method item or length instead of your element.

这篇关于Safari 原生代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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