Jquery对象innerHTML在Firefox中工作但不是IE?

时间:2021-09-29 20:35:29

In summary I have a xml string which I use jQuery to find some children of a specific node.

总之,我有一个xml字符串,我使用jQuery来查找特定节点的一些子节点。

These children and their values are used to build a HTML select box and append it to a webpage.

这些子项及其值用于构建HTML选择框并将其附加到网页。

The XML

XML

<!--- Another node level !-->          
       <Salutation restricted="no" type="dropdownBox" tooltip="Select a title for the customer" required="yes" size="6">
           <value>Mr</value>
           <value>Sir</value>
           <value>Mrs</value>
           <value>Miss</value>
           <value>Lord</value>
       </Salutation>

The Code

代码

    function dropdownBuilder( xml, element, id ) {
       // find node with specific name and get its children
       selection = $("<div>" + xml + "</div>").find(element).children();
       console.log( selection );
       // generate a select box
       var selectBox = "<select id=\"" + id + "\"> ";
       for ( var j = 0; j < selection.length; j++ ) {
        selectBox += "<option value=\"" + selection[j].innerHTML + "\">"
                + selection[j].innerHTML + "</option>";
       }
       selectBox += "</select>";

       // return html
       return selectBox;
}

Output FIREFOX

输出FIREFOX

<select id="dropdownBox1" data-hasqtip="true" aria-describedby="qtip-1">
    <option value="Mr">Mr</option>
    <option value="Sir">Sir</option>
    <option value="Mrs">Mrs</option>
    <option value="Miss">Miss</option>
    <option value="Lord">Lord</option>
</select>

Output IE

输出IE

<select id="dropdownBox1" data-hasqtip="true" jQuery16309410884371447445="60"/>

In the console log in IE it just says "LOG: [object Object] "

在IE中的控制台日志中它只是说“LOG:[object Object]”

I have tried changing to output using .val() , .value , .data , .attr

我尝试使用.val(),. value,。data,.attr更改为输出

Any idea why this works fine in FF but not in IE?

知道为什么这在FF中工作正常而在IE中没有?

JSFIDDLE

的jsfiddle

http://jsfiddle.net/BA7u6/1/

http://jsfiddle.net/BA7u6/1/

Thanks

谢谢

2 个解决方案

#1


1  

You can replace the for loop in you code with this and it should work :

你可以用你的代码替换你的代码中的for循环,它应该工作:

selection.each(function(index) {
    var text = $(this).text();
    selectBox += "<option value=\"" + text + "\">" + text + "</option>";
});

#2


1  

you should use an xml parser to do what you are after

你应该使用xml解析器来做你想做的事情

see this: http://api.jquery.com/jQuery.parseXML/

看到这个:http://api.jquery.com/jQuery.parseXML/

#1


1  

You can replace the for loop in you code with this and it should work :

你可以用你的代码替换你的代码中的for循环,它应该工作:

selection.each(function(index) {
    var text = $(this).text();
    selectBox += "<option value=\"" + text + "\">" + text + "</option>";
});

#2


1  

you should use an xml parser to do what you are after

你应该使用xml解析器来做你想做的事情

see this: http://api.jquery.com/jQuery.parseXML/

看到这个:http://api.jquery.com/jQuery.parseXML/