简体   繁体   English

使用javascript更改标签属性的值

[英]change value of attributes of a tag using javascript

I have the following HTML generated by Drupal 我有以下Drupal生成的HTML

<fieldset id="webform-component-lunchset" class="webform-component-fieldset form-wrapper" style="">
    <div id="webform-component-lunchset--lunch" class="form-item webform-component webform-component-radios webform-container-inline" style="">
        ...Radio Button div tags here
    </div>
</fieldset>
<fieldset id="webform-component-dinnerset" class="webform-component-fieldset form-wrapper" style="display: none;">
    <div id="webform-component-dinnerset--dinner" class="form-item webform-component webform-component-radios webform-container-inline" style="display: none;">
        ...Radio Button div tags here
    </div>
</fieldset>

My intent is to allow the user to choose a meal time based on the hour of the day when he initiates a booking. 我的目的是允许用户根据他发起预订的一天中的小时来选择用餐时间。 To do this I noticed that I have to change style="display: none;" 为此,我注意到我必须更改style =“ display:none;”。 for the field set I want to hide and style="" for the one I want displayed. 我要隐藏的字段集,为我要显示的字段设置style =“”。 I have the following Javascript statements to change the style attribute of the DIV tags but it doesn't seem to work. 我有以下Javascript语句来更改DIV标签的样式属性,但它似乎不起作用。 Any help would be appreciated. 任何帮助,将不胜感激。

window.onload = (function() { 
    var today = new Date();
    var day = today.getDay();
    var hour = today.getHours();
    var lunchtime1 = document.getElementById('webform-component-lunchset');
    var lunchtime2 = document.getElementById('webform-component-lunchset--lunch');
    var dinnertime1 = document.getElementById('webform-component-dinnerset');
    var dinnertime2 = document.getElementById('webform-component-dinnerset--dinner');
    if (hour >= 15 && hour < 22) {
        lunchtime1.style="display: none;";
        lunchtime2.style="display: none;";
        dinnertime1.style=" ";
        dinnertime2.style=" ";
        sundaytime1.style="display: none;";
    }
});

Not sure why it isn't working, would appreciate any help. 不知道为什么它不起作用,将不胜感激。 Thank you in advance 先感谢您

When you want to use the style property of a reference to a node, you're using the DOM API. 当您要使用对节点的引用的style属性时,您将使用DOM API。 A DOMElement object has a property, called style , which has to be an object, too. DOMElement对象具有一个称为style的属性,该属性必须是一个对象。 So either write: 所以写:

lunchtime1.style.display = 'none';

Or use the setAttribute method - so you can set the entire style attribute in one go: 或使用setAttribute方法-这样就可以一次性设置整个样式属性:

lunchtime1.setAttribute('style','display: block; background-color: #F00;');

That should do the trick... 这应该够了吧...

note: 注意:
As Tim Down pointed out, old IE's don't do setAttribute as well as decent browsers, so the first approach is the most reliable. 正如Tim Down所指出的那样,旧版IE的setAttribute效果不如浏览器好,因此第一种方法是最可靠的。 I'm looking into the matter. 我正在调查这件事。 IE doesn't allow you to write someNode.setAttribute('onclick', 'clickHandler()'); IE不允许您编写someNode.setAttribute('onclick', 'clickHandler()'); for example. 例如。
I've also encountered issues that seem to affect only certain elements (often form elements), but when it comes to setting the style attribute, I can't be sure... Either you can't set certain attributes, or you might not be able to set any attribute on some elements. 我还遇到了似乎仅影响某些元素(通常是表单元素)的问题,但是当涉及到设置样式属性时,我不确定...要么无法设置某些属性,要么无法在某些元素上设置任何属性。 Whatever... I'll update this answer once I know more about this. 无论如何...一旦我对此有所了解,我将更新此答案。 (currently no windows boxes around, so no IE to test) (当前没有窗口框,因此没有要测试的IE)

dinnertime1.style.display="inline";

I think style="<string>" isn't possible. 我认为style="<string>"是不可能的。 You have to write lunchtime1.style.display = "none" . 您必须编写lunchtime1.style.display = "none" Style is a "full" js-object. 样式是一个“完整”的js对象。

Anyway, is there a reason not to use jQuery? 无论如何,是否有理由不使用jQuery?

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

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