Javascript(object.innerhtml)与IE不兼容

时间:2013-07-07 14:10:14

标签: javascript internet-explorer innerhtml

我创建了一个脚本,根据所选类别更改组合框。问题是该脚本适用于除Internet Explorer(版本7+)之外的所有其他浏览器。我没有收到错误消息,这表明IE不喜欢object.innerhtml。我该怎么做才能解决这个问题?

工作示例:http://adcabinetsales.com/style-chooser.html

function ChangeCabinetCollection() {
    if (document.getElementById("cabinet_collection").value == "broughton") {
        // COPY VALUES
        var first = document.getElementById('broughton_styles');
        var options = first.innerHTML;
        var second = document.getElementById('cabinet_selector');

        // REPLACE VALUES
        second.innerHTML = options;

        // CHANGE CABINET IMAGE TO BE IN THE COLLECTION OF CHOICE
        changeDoor("cabinet_selector");
    } else if (document.getElementById("cabinet_collection").value == "specialty") {
        // COPY VALUES
        var first = document.getElementById('cabinet_style');
        var options = first.innerHTML;
        var second = document.getElementById('cabinet_selector');

        // REPLACE VALUES
        second.innerHTML = options;

        // CHANGE CABINET IMAGE TO BE IN THE COLLECTION OF CHOICE
        changeDoor("cabinet_selector");
    }
}

function ChangeGraniteCollection() {
    if (document.getElementById("granite_collection").value == "new_arrivals") {
        // COPY VALUES
        var first = document.getElementById('granite_new');
        var options = first.innerHTML;
        var second = document.getElementById('granite_selector');

        // REPLACE VALUES
        second.innerHTML = options;

        // CHANGE CABINET IMAGE TO BE IN THE COLLECTION OF CHOICE
        changeGranite("granite_selector");
    } else if (document.getElementById("granite_collection").value == "Specialty Styles") {
        // COPY VALUES
        var first = document.getElementById('specialty_granite_styles');
        var options = first.innerHTML;
        var second = document.getElementById('granite_selector');

        // REPLACE VALUES
        second.innerHTML = options;

        // CHANGE CABINET IMAGE TO BE IN THE COLLECTION OF CHOICE
        changeGranite("granite_selector");
    }
}

2 个答案:

答案 0 :(得分:0)

一般来说不是.innerHTML;它正在使用<select>元素导致你出现问题。有几种方法可以解决问题。一个简单的方法是将整个<select>元素与选项列表一起替换。也就是说,如果您的HTML看起来像这样:

<select name=whatever id=whatever>
  <option value=1>Hello
  <option value=2>World
</select>

然后你可以改变它:

<span class=select-wrapper>
  <select name=whatever id=whatever>
    <option value=1>Hello
    <option value=2>World
  </select>
</span>

然后只需替换.innerHTML周围<span>包装的<select>

解决问题的另一种方法是构建一个Option实例数组并更新<select>元素的“options”属性。

答案 1 :(得分:0)

我不确定为什么你会遇到IE7 +的问题,我没有微软产品可以测试,但可能是这个错误:

BUG: Internet Explorer Fails to Set the innerHTML Property of the Select Object

但是接受@Pointy回答,那么你可以像这样替换select,而不是包装元素,使用Node.cloneNode Node.replaceChild。我相信这是跨浏览器。

HTML

<select name="granite_collection" id="granite_collection">
    <option selected="selected">Choose a Collection</option>
    <option value="new_arrivals">Granite: New Arrivals</option>
    <option value="Specialty Styles">Granite: Specialty Styles</option>
</select>
<select name="granite_selector" id="granite_selector"></select>
<select name="cabinet_style" id="cabinet_style" style="visibility:hidden;">
    <option value="images/doorstyles/Brandy wine.jpg">Brandy Wine</option>
    <option value="images/doorstyles/Canvas.jpg">Canvas</option>
    <option value="images/doorstyles/Country Linen.jpg">Country Linen</option>
    <option value="images/doorstyles/Expresso.jpg">Expresso</option>
    <option value="images/doorstyles/Mocha glazed.jpg">Mocha Glazed</option>
    <option value="images/doorstyles/Shaker.jpg">Shaker</option>
    <option value="images/doorstyles/Tahoe.jpg">Tahoe</option>
    <option value="images/doorstyles/Charleston Antique White.jpg">Charleston Antique White</option>
    <option value="images/doorstyles/Charleston Cherry.jpg">Charleston Cherry</option>
    <option value="images/doorstyles/Charleston Saddle.jpg">Charleston Saddle</option>
    <option value="images/doorstyles/Java Shaker.jpg">Java Shaker</option>
    <option value="images/doorstyles/Savannah Toffee.jpg">Savannah Toffee</option>
    <option value="images/doorstyles/White Shaker.jpg">White Shaker</option>
    <option value="images/doorstyles/York Chocolate.jpg">York Chocolate</option>
    <option value="images/doorstyles/York Antique White.jpg">York Antique White</option>
    <option value="images/doorstyles/Dillon1.jpg">Dillon1</option>
    <option value="images/doorstyles/Dillon2.jpg">Dillon2</option>
</select>

的Javascript

var graniteCollection = document.getElementById("granite_collection"),
    graniteSelector = document.getElementById("granite_selector"),
    cabinetStyle = document.getElementById("cabinet_style");

graniteCollection.onchange = function () {
    var clone = cabinetStyle.cloneNode(true);

    clone.id = graniteSelector.id;
    clone.name = graniteSelector.name;
    clone.style.visibility = "";
    graniteSelector.parentNode.replaceChild(clone, graniteSelector);
}

jsfiddle