简体   繁体   English

事件侦听器以形成标签

[英]event listeners to form label

So I am trying to make this function use event listeners to the form's label. 因此,我试图使此函数对表单的标签使用事件侦听器。 Its supposed to takes the element's id value as its argument, then give each label an id value of id + 'Label'. 它应该以元素的id值作为其参数,然后为每个标签赋予id值id +'Label'。 It is currently not doing anything when I call it. 我打电话时它什么也没做。 U and this.$U is other code to shorten the code. U和this。$ U是缩短代码的其他代码。

    addTooltipHandlers: function(id) {
    'use strict';

    // Get the form element reference:
    var newId = this.$(label.id);

    newId.onmouseover = showTooltip;
    newId.onmouseout = hideTooltip;
    newId.onfocus = showTooltip;
    newId.onblur = hideTooltip;

}, // End of addTooltipHandler() function.

I am calling it in another js file using. 我正在使用另一个js文件中调用它。

U.addTooltipHandler('someLabel');

Html code I am trying to use it on html代码,我想在上面使用

<div class="two"><label for="phone">Phone Number<span class="tooltip">In the format XXX XXX XXXX or XXX-XXX-XXXX or XXXXXXXXXX.</span></label><input type="text" name="phone" id="phone" required></div>

Try this: 尝试这个:

addTooltipHandlers: function(id) {
    'use strict';

    // Define the new ID value:
    var newId = this.$(id + 'label');

    newId.onmouseover = showTooltip;
    newId.onmouseout = hideTooltip;
    newId.onfocus = showTooltip;
    newId.onblur = hideTooltip;

},

There's no need to get the element from id , you're not doing anything with that element. 无需从id获取元素,您无需对该元素做任何事情。 You just want to concatenate the given ID with label , and use that to find the new element to attach the handlers to. 您只想将给定的ID与label串联起来,并使用它来查找将处理程序附加到的新元素。

And the second onmouseover should be onmouseout . 第二次onmouseover应该是onmouseout

For this to work, you need to give IDs to your labels: 为此,您需要在标签上添加ID:

<label for="phone" id="phonelabel">

Then you can call: 然后,您可以致电:

U.addToolTipHandlers('phone');

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

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