繁体   English   中英

我无法在Firefox中通过“ for”属性访问标签,但在Chrome中可以访问

[英]I can't access labels via “for” attribute in Firefox, but can in Chrome

我有一个需要动态填充标签的表单(由Google Closure生成)。 我可以在chrome中使用它,但是当我在Firefox中尝试时无法使用,并且失败并显示以下错误:

TypeError: this.document.getElementById(...)[$i].labels is undefined
....document.getElementById('dialog-form')[$i].labels[0].innerHTML = "$" + varNewPr...

通过Firebug控制台检查时,出现以下错误:

>>> this.document.getElementById('dialog-form')[4]
<input id="price1" type="radio" value="1" percentage="90" adoptname="90" name="price">
>>> this.document.getElementById('dialog-form')[4].labels
undefined

但是,使用chrome中的本机调试控制台可以正常工作(value =和takenname =的不同值是由于动态重用了表单)

>this.document.getElementById('dialog-form')[4]
<input type=​"radio" name=​"price" adoptname=​"180" id=​"price1" percentage=​"90" value=​"2">​
>this.document.getElementById('dialog-form')[4].labels
[<label class=​"adopt-label" for=​"price1">​$0​</label>​]

这是从Google Closure编译器出来的html表单代码:

// This file was automatically generated from basic.soy.
// Please don't edit this file by hand.

if (typeof examples == 'undefined') { var examples = {}; }
if (typeof examples.simple == 'undefined') { examples.simple = {}; }


examples.simple.adoptForm = function(opt_data, opt_ignored) {
return '<div class="adopt-general">
<div class="adopt-header">

....

<ul class="adopt-list">
<li><label>Tell me if the price drops below:</label>
<li><input type="radio" name="price" adoptname="$' + soy.$$escapeHtml(Math.round(opt_data.price * 0.9)) + '" id="price1" percentage="90" value="' + soy.$$escapeHtml(opt_data.itemNumber) + '" />
<label class="adopt-label" for="price1">$' + soy.$$escapeHtml(Math.round(opt_data.price * 0.9)) + '</label>
<input type="radio" name="price" adoptname="$' + soy.$$escapeHtml(Math.round(opt_data.price * 0.8)) + '" id="price2" percentage="80" value="' + soy.$$escapeHtml(opt_data.itemNumber) + '" />
<label class="adopt-label" for="price2">$' + soy.$$escapeHtml(Math.round(opt_data.price * 0.8)) + '</label>
<input type="radio" name="price" adoptname="$' + soy.$$escapeHtml(Math.round(opt_data.price * 0.7)) + '" id="price3" percentage="70" value="' + soy.$$escapeHtml(opt_data.itemNumber) + '" />
<label class="adopt-label" for="price3">$' + soy.$$escapeHtml(Math.round(opt_data.price * 0.7)) + '</label></ul>

    ...;

};

javascript代码在这里:

for(var $i = 0; $i < $myDialogLength; $i++){
    this.document.getElementById('dialog-form')[$i].setAttribute("value",skuNumber);
    this.document.getElementById('dialog-form')[$i].checked = false;
    if(this.document.getElementById('dialog-form')[$i].getAttribute("percentage") !== null){
        varIdNum = this.document.getElementById("dialog-form")[$i].getAttribute("percentage");
        var varNewPrice = (varIdNum*price/100);
        this.document.getElementById('dialog-form')[$i].setAttribute("adoptname",varNewPrice);            
        this.document.getElementById('dialog-form')[$i].labels[0].innerHTML = "$" + varNewPrice;
    }
....
}

代码的最后一行在Firefox中引发错误。 我也在使用JQuery,并且没有太多的JavaScript经验,因此我对非最佳代码表示歉意。 任何帮助表示赞赏,谢谢!

我想这意味着Firefox不支持.labels属性。

尝试以下方法:

document.querySelector("[for='"+ID+"']").innerHTML = "$"+varNewPrice;

只需确保ID是对您元素ID的某种引用即可。 您当前的代码确实很奇怪...看起来几乎您有多个具有相同ID的元素...

看起来:

this.document.getElementById('dialog-form')[4]

是对具有id="price1" ... name="price"的输入的引用。 您还可以使用以下方法获得相同的元素:

this.document.forms['dialog-form'].price

但是无论如何, HTMLInputElement的接口没有包含标签属性。 您期望它做什么,返回关联的标签(如果有的话)?

您需要说出要做什么,然后发布DOM的相关部分,最好以HTML的形式发布。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM