简体   繁体   English

检查Chrome中的元素未显示所选选项

[英]Inspect element in Chrome not showing selected option

When I use Chrome's Inspect Element feature to view a <select> element that I've programmatically set the selected option on, the selected <option> doesn't show selected="selected" . 当我使用Chrome的Inspect Element功能查看我以编程方式设置所选选项的<select>元素时,所选的<option>不会显示selected="selected" Everything works properly, I just can't see which options are selected in the inspector view. 一切正常,我只是看不到在检查器视图中选择了哪些选项。

Is this the proper behavior? 这是正确的行为吗? It seems like not only should the selected element be updated in the internal representation of the DOM, but that selected="selected" should be added to the visual representation as well. 似乎不仅应该在DOM的内部表示中更新所选元素,而且还应该将selected="selected"添加到视觉表示中。

Here's an example using several different ways to set the selected property of an <option> : http://jsfiddle.net/ScTTY/ 以下是使用几种不同方法设置<option>selected属性的示例: http//jsfiddle.net/ScTTY/

Essentially, I'm using variations on this code: 基本上,我正在使用此代码的变体:

    var current = new Date().getFullYear();
    var year1 = this.$("select.year1");
    for (var i=0; i<100; i++) {
        var option = $("<option>",{
            value: current - i,
            text: current - i,
            selected: (i==17 ? "selected" : "")
        });
        year1.append(option);
    }

However, I use different ways of setting the selected option: 但是,我使用不同的方式设置selected选项:

        selected: (i==17 ? true : false)

        if (i==17) option.attr("selected","selected");

        if (i==17) option[0].selected = true;

        if (i==17) option[0].selected = "selected";

All of these methods create a <select> containing the years 1912 to 2011 with 1994 selected. 所有这些方法都创建了一个<select>其中包含<select> 1994年的1912年至2011年。

When in the Elements tab, you see the Style properties on the right. 在“元素”选项卡中,您会在右侧看到“样式”属性。 Under the Styles drawer is a Properties drawer. “样式”抽屉下是“属性”抽屉。 Click on the selected option and the Property will indicate that it is selected. 单击所选选项,属性将指示它已被选中。

It's a good question though - I would expect to see it in the HTML. 这是一个很好的问题 - 我希望在HTML中看到它。 I can only offer that it works the same in Firebug so it looks to be standard behavior. 我只能提供它在Firebug中的相同功能,因此它看起来是标准行为。

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

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