简体   繁体   English

如何访问正确的XUL标度更改值

[英]How do I access the correct XUL scale changed value

I'm building a 'classic' Firefox XUL overlay extension. 我正在构建一个'经典'Firefox XUL覆盖扩展。 In this extension I want to include a XUL scale element . 在这个扩展中,我想要包含一个XUL scale元素

I'm trying to read out the updated value of the scale element, if the user changed the position of the slider. 如果用户更改了滑块的位置,我正在尝试读出scale元素的更新值。 However, I can't get the updated value — the value keeps staying at the starting position. 但是,我无法获得更新的值 - 该值始终保持在起始位置。

<edit> <编辑>
One important thing to note though is that you need the Classic Theme Restorer add-on , and activate View > Toolbars > Add-on Bar (for Firefox 29+), to get my test case to show up in the status bar element, because that add-on's Add-on Bar restores the status bar. 需要注意的一件重要事情是,您需要经典主题恢复器附加组件 ,并激活View > Toolbars > Add-on Bar (适用于Firefox 29+),以使我的测试用例显示在状态栏元素中,因为该附加组件的附加栏可恢复状态栏。
</edit> </编辑>

The contents of slider/chrome/content/slider.xul are: slider/chrome/content/slider.xul是:

<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="chrome://global/skin/"?>
<?xml-stylesheet type="text/css" href="slider.css"?>
<overlay id="overlay-slider" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <script type="application/x-javascript" src="slider.js"/>
  <statusbar id="status-bar">
    <scale id="slider" width="200" movetoclick="true" increment="10"/>
  </statusbar>
</overlay>

The contents of slider/chrome/content/slider.js are: slider/chrome/content/slider.js是:

var Logger = {
  log: Application.console.log
}

var Slider = {
  onChange: function( event ) {
    // keeps giving me 0, in stead of the updated value
    Logger.log( document.getElementById( 'slider' ).value );
  }
}

var onWindowLoad = function( event ) {
  window.removeEventListener( 'load', onWindowLoad, false );

  var slider = document.getElementById( 'slider' );
  slider.addEventListener( 'change', Slider.onChange, false );
}

window.addEventListener( 'load', onWindowLoad, false );

It appears that the scale element behaves rather erratic, because I've also tried adding a XUL textbox element to the xul overlay, like so: 似乎scale元素表现得相当不稳定,因为我也尝试将xUL textbox元素添加到xul叠加层,如下所示:

...
<scale id="slider" width="200" movetoclick="true" increment="10"/>
<textbox id="slider-value" readonly="true" observes="slider"/>
...

In this case the textbox updates appropriately and I can use it as a proxy to read out the scale value by changing the relevant javascript, to: 在这种情况下, textbox相应更新,我可以将其用作代理,通过更改相关的javascript来读取scale值,以:

// changed 'slider' to 'slider-value'
Logger.log( document.getElementById( 'slider-value' ).value );

However, when I add min , max (and possibly other) attributes to the scale element the textbox element won't update anymore either and keeps staying at the min value, all of a sudden. 但是,当我向scale元素添加minmax (和可能的其他)属性时, textbox元素也不会再更新,并且会一直保持min

I've also tried a couple of variations of accessing data from the onchange event that gets passed to the event-handler, like: 我还尝试了一些从onchange事件访问数据的变体,这些变量被传递给事件处理程序,如:

Logger.log( event.value );
Logger.log( event.target.value );
Logger.log( event.target._sliderElement.value );
Logger.log( event.currentTarget.value );
... etc.

And on inspecting various properties of the event, I also noticed that the _userChanged property stays false , when I expect this to be true , since the scale was not altered programmatically, but by the user. 在检查事件的各种属性时,我还注意到_userChanged属性保持为false ,当我希望这是true ,因为scale没有以编程方式改变,而是由用户改变。

Are these bugs in the scale element? 这些错误是否在scale元素中? Or am I missing some vital information here, to read out the correct values? 或者我在这里遗漏了一些重要信息,以便读出正确的值?


I'm testing the extension in an instance of Firefox with -no-remote -P development arguments with an extension proxy file , on Mac OS X 10.6.8 and Xubuntu 14.04 (although I doubt that this is relevant). 我正在使用扩展代理文件扩展代理文件 -no-remote -P development参数在Firefox的实例中测试扩展,在Mac OS X 10.6.8和Xubuntu 14.04上(尽管我怀疑这是相关的)。


<edit> <编辑>
I did some more testing, after reading Noitidart's answer , and found out that overlaying the scale element in the status bar element appears to be the cause of my problems. 在阅读了Noitidart的回答之后,我做了一些测试,发现在状态栏元素中覆盖scale元素似乎是我遇到问题的原因。 All works as intended now, if I overlay my scale and observing textbox elements directly in either the Add-on Toolbar or the Additional Toolbar of the Classic Theme Restorer add-on, like so: 如果我覆盖我的scale并直接在附加工具栏或经典主题修复程序附加工具的附加工具栏中观察textbox元素,则所有工作都按预期工作,如下所示:

 <?xml version="1.0"?> <?xml-stylesheet type="text/css" href="chrome://global/skin/"?> <?xml-stylesheet type="text/css" href="slider.css"?> <overlay id="overlay-slider" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> <script type="application/x-javascript" src="slider.js"/> <toolbar id="ctraddon_addon-bar"> <!-- or: id="ctraddon_extra-bar" --> <scale id="slider-simple" class="slider" movetoclick="true" increment="10"/> <textbox id="slider-simple-textbox" class="textbox" readonly="true" observes="slider-simple"/> <scale id="slider-complex" class="slider" movetoclick="true" increment="10" min="20" max="80"/> <textbox id="slider-complex-textbox" class="textbox" readonly="true" observes="slider-complex"/> </toolbar> </overlay> 

</edit> </编辑>

Ok I was able to fix the logging to the console. 好的,我能够将日志记录修复到控制台。 It properly gets the value. 它恰当地获得了价值。 I had to use .getAttribute('value') instead of .value . 我不得不使用.getAttribute('value')而不是.value

var Slider = {
  onChange: function( event ) {
    // read the slider value
    Logger.log( event.target.id + '.value = ' + event.target.getAttribute('value') );

    // try to read the associated textbox value (as a proxy to the slider value)
    var textBox = document.getElementById( event.target.id + '-textbox' );
    Logger.log( textBox.id + '.value = ' + textBox.getAttribute('value') );
  }
}

But now the weird thing is, the slider-complex textbox doesnt update even though the value is updating. 但现在奇怪的是,即使值正在更新, slider-complex文本框也不会更新。

I don't know man I looked up on mdn the observes attribute and can't find any information on it. 我不知道的人,我抬头对MDN的observes属性,并不能找到任何信息。

<scale id="slider-simple" class="slider" movetoclick="true" increment="10"/>
<textbox id="slider-simple-textbox" class="textbox" readonly="true" observes="slider-simple"/>

<scale id="slider-complex" class="slider" movetoclick="true" increment="10" min="20" max="80"/>
<textbox id="slider-complex-textbox" class="textbox" readonly="true" observes="slider-complex"/>

that observes="slider-complex" is a new one on me. observes="slider-complex"对我来说是新的。 never knew about that, can you explain it to me, where did you find this? 从来不知道,你能解释一下吗,你在哪里找到了这个? ill update mdn and maybe i can help you more. 生病了更新mdn,也许我可以帮助你更多。

Weird thing is though, that slider-simple works fine in console logging with .value and also the textbox updates appropriately. 奇怪的是, slider-simple在使用.value控制台日志记录中正常工作,并且文本框也适当更新。 real weird. 真奇怪。 but slider-simple console logging also works with .getAttribute('value') so we just need to figure out whtats up with complex one. slider-simple控制台日志记录也适用于.getAttribute('value')所以我们只需要弄清楚复杂的一个。 if i remove the min and max attributes from complex-slider it works fine but i cant explain this, something weird is going on. 如果我从complex-slider删除minmax属性它工作正常,但我无法解释这一点,奇怪的是正在进行。

The MDN slider page says this for the value ATTRIBUTE: https://developer.mozilla.org/en-US/docs/XUL/Attribute/value MDN滑块页面显示值为ATTRIBUTE: https//developer.mozilla.org/en-US/docs/XUL/Attribute/value

For those elements, setAttribute("value", myValue) and getAttribute("value") do not access or affect the contents displayed to the user. 对于这些元素,setAttribute(“value”,myValue)和getAttribute(“value”)不访问或影响显示给用户的内容。

it has a value PROPERTY which is used for displaying the value, in the text box. 它在文本框中有一个值PROPERTY,用于显示值。

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

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