简体   繁体   English

Javascript自动计算无法在FF中运行,在IE和Chrome中可以正常运行

[英]Javascript auto-calcs won't run in FF, works fine in IE and Chrome

I have the following asp.net textbox defined in markup: 我在标记中定义了以下asp.net文本框:

<div class="OptionSelect2">
    <asp:TextBox ID="txtCalculateWeightRollSize" CssClass="_100" runat="server"></asp:TextBox>
</div>

In the code-behind, on Page_Load, I am binding the onblur event: 在后面的代码中,在Page_Load上,我绑定了onblur事件:

txtCalculateWeightRollSize.Attributes.Add("onblur", "Javascript:RollBWghtPCutoffNWebsCalcs();")

The Javascript (reformatted to more easily to fit into a question format), is as such: 这样的Javascript(经过重新格式化以便更容易地适应问题格式)是这样的:

function RollBWghtPCutoffNWebsCalcs() { ZeroPctLbs(); }
function ZeroPctLbs() {
    //Get the values
    var rollSize = document.getElementById("<% =txtCalculateWeightRollSize.ClientID %>").value.replace("$", "").replace("%", "");
        //Get other controls' values

    //Make them numbers, if necessary
    rollSize = +rollSize;
        //convert other controls

    //Calculate and assign the result
    var result = //math;
    document.getElementById("<% =lblCalculateWeightZeroPercentLbsValue.ClientID %>").innerText = result;
}

This works without a problem in both IE and Chrome. 这在IE和Chrome中都没有问题。 However, in Firefox, it fails. 但是,在Firefox中,它会失败。 In firebug (which I have not used until today), it appears that the onblur event fires, but stepping into the code, when it should be calling RollBWghtPCutoffNWebsCalcs(), it passes over it as though the calling doesn't exist. 在Firebug(直到今天我还没有使用过)中,似乎触发了onblur事件,但是进入了代码,当它应该调用RollBWghtPCutoffNWebsCalcs()时,它会像没有调用那样经过它。 If I place a breakpoint directly inside any of the Javascript functions, it is never triggered. 如果将断点直接放在任何Javascript函数内部,则永远不会触发该断点。

The textbox appears to be rendered correctly in HTML: 该文本框似乎已正确显示为HTML:

<input name="ctl00$MainContent$txtCalculateWeightRollSize" type="text" id="MainContent_txtCalculateWeightRollSize" class="_100" onblur="Javascript:RollBWghtPCutoffNWebsCalcs();" />

Here is a JSFiddle that I believe shows the issue. 我相信这是一个JSFiddle ,显示了问题所在。 The onblur doesn't work in any browser in the fiddle, but that would be consistent with my experience with Firefox issues in the past. onblur不能在小提琴中的任何浏览器中使用,但这与我过去在Firefox问题上的经验是一致的。 It does work in Chrome and IE when rendered directly, and JSHint indicates that the Javascript is valid. 直接呈现时,它确实可以在Chrome和IE中工作,并且JSHint表示Javascript有效。

I suspect something I'm doing isn't completely standard, as Firefox (by my understanding) is pretty much a standards-based web experience. 我怀疑我正在做的事情不是完全标准的,因为Firefox(据我了解)几乎是一种基于标准的Web体验。 WHY WON'T IT RUN? 为什么不运行? Please. 请。

Sometimes it's the simple things... 有时候这很简单...

.innerText isn't supported by FireFox. FireFox不支持.innerText。 Use innerHTML instead. 请改用innerHTML。

Once I stopped using Firebug and switched to FF's built-in debugger, I was able to actually trace my code - I guess I just don't know how to use Firebug. 一旦我停止使用Firebug并切换到FF的内置调试器,我便能够真正跟踪我的代码-我想我只是不知道如何使用Firebug。 The odd thing that threw me off was that FF would get the values FROM the labels using innerText, and would write them in such a way that subsequent function calls could read the written values, but it would not render the innerText. 令我失望的是,FF将使用innerText从标签中获取值,并以后续函数调用可以读取写入的值的方式写入它们,但不会呈现innerText。

tldr; tldr;

Use innerHTML to work with label text in Javascript. 使用innerHTML使用Javascript中的标签文本。

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

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