jQuery html 属性在 IE 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/412734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:06:57  来源:igfitidea点击:

jQuery html attribute not working in IE

jqueryhtmlforms

提问by

I am using country and state dropdowns in my form. Whenever the user selects the country in the dropdown, the respective states of the country will populate in the states dropdown. I am populating the states in the dropdown using AJAX call. The problem is that the states get populated in Mozilla but it doesn't work in IE. I guess there is some problem in jQuery while loading the states in the states dropdown. The jQuery code i am using is

我在表单中使用国家和州下拉列表。每当用户在下拉列表中选择国家/地区时,该国家/地区的相应州将填充到州下拉列表中。我正在使用 AJAX 调用填充下拉列表中的状态。问题是状态在 Mozilla 中填充,但它在 IE 中不起作用。我猜在状态下拉列表中加载状态时,jQuery 中存在一些问题。我正在使用的 jQuery 代码是

$('select#edit-country').change(function(e) {

    $.getJSON("loadContactUsStates",{id: $(this).val(), ajax: 'true'}, function(j){
        var options = '';

        for (var i = 0; i < j.length; i++) { 
            options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';

        }

    <!-----I guess the problem is with the below line------------------>
       $("select#edit-state").html(options);

    })//end json

});

回答by Kieron

Try using appendinstead of the htmlmethod as detailed in this post.

尝试使用附加,而不是HTML作为该具体方法

edit

编辑

Actually, I've just run into this problem myself. For me the answer was to call emptyfirst, then appendwhich has the same effect (I think?) as using the html method.

实际上,我自己刚刚遇到了这个问题。对我来说,答案是先调用empty,然后append与使用 html 方法具有相同的效果(我认为?)。

回答by Jukums

Also (in my case) check if you have valid html, I had mismatched tags, and it worked in firefox and didn't in IE(6-8)

另外(在我的情况下)检查你是否有有效的 html,我有不匹配的标签,它在 firefox 中工作而在 IE(6-8) 中没有

回答by seaingsky

add to .ajax options

添加到 .ajax 选项

cache: false,

缓存:假,

回答by egallardo

This is what worked for me: (I tested on IE7 - IE9, and Chrome)

这对我有用:(我在 IE7 - IE9 和 Chrome 上进行了测试)

It looks like the trick for IE is to use a DIV wrapper

看起来 IE 的诀窍是使用 DIV 包装器

Original html:

原始html:

<div  id="dynamicMenu"></div>

jQuery script:

jQuery 脚本:

$.ajax({

            url: "/myapp/myajaxUrl.jsp",
            type: "GET",
            dataType: "html",
            async: false,
            success: function(msg) {
                $("#dynamicMenu").html(msg);
            });

Where msg is something like:

其中 msg 是这样的:

<TABLE>
<TBODY>
 <TR>
  <TD><LABEL for="dropdown1">OS type:</LABEL></TD>
  <TD>
   <SELECT id="dropdown1">
    <OPTION selected value="">Select OS</OPTION>
    <OPTION value="WIN">Windoze</OPTION>
    <OPTION value="UX">Unix</OPTION>
    <OPTION value="LX">Linux</OPTION>
   </SELECT>
  </TD>
 </TR>
</TBODY>
</TABLE>

I tried .empty() .html() to no avail but the above worked great!

我试过 .empty() .html() 无济于事,但上面的效果很好!

回答by user1423372

The following method solved the problem for me:

以下方法为我解决了问题:

$('Element ID OR Class').empty().html(string);

First use empty()& then set html using html()

首先使用empty()& 然后使用设置 htmlhtml()

回答by tiberdoo

After hours of frustration I realized that IE does not support jquery attribute functions for html5 elements other than div. I was trying to do this:

经过数小时的沮丧,我意识到 IE 不支持 div 以外的 html5 元素的 jquery 属性函数。我试图这样做:

success: function (response, textStatus, XMLHttpRequest) {
    $response = $(response.replace(/\t/g, " "));
    $responseHTML = $response.find("#pageContainer").html();
    $container.html($responseHTML);

For this element:

对于这个元素:

<nav id="pageContainer" class="content">
</nav>

By changing it to this it solved the problem:

通过将其更改为这个,它解决了问题:

<nav>
    <div id="pageContainer" class="content">
    </div>
</nav>

回答by Hasan Cem Cerit

If you are parsing xml using jquery, and you need html() in IE, try this:

如果您正在使用 jquery 解析 xml,并且您需要在 IE 中使用 html(),请尝试以下操作:

var content = ($.browser.msie) ? $(this).context.xml : $(this).html();

var content = ($.browser.msie) ? $(this).context.xml : $(this).html();

This solved my problem. I hope it will help someone too.

这解决了我的问题。我希望它也能帮助别人。

Greetings.

你好。

回答by 99grad

I had the same problem after receiving an AJAX HTML-Request with the function jQuery.ajax()and then trying to parse the result with jQuery( html_result_data ). The solution was to strip the header and all tabs and "returns" in the html_result_data like this:

在收到带有函数jQuery.ajax()的 AJAX HTML 请求,然后尝试使用jQuery( html_result_data )解析结果后,我遇到了同样的问题。解决方案是在 html_result_data 中删除标题和所有选项卡并“返回”,如下所示:

success: function( data ) {
   // God this is ugly
   data = data.split("<body>")[1].split("</body>")[0];
   data = data.split("\t").join("").split("\r").join("").split("\n").join("");                      
   data = jQuery( data );
   ...
}

回答by duttyman

I had the same problem and found that the simplest solution was to use the replaceWith()function.

我遇到了同样的问题,发现最简单的解决方案是使用该replaceWith()功能。

回答by gordon

Jukums' comment led me to try turning off ColdFusion debug output and then .html() worked for me in IE8. To force debug output off:

Jukums 的评论让我尝试关闭 ColdFusion 调试输出,然后 .html() 在 IE8 中为我工作。强制关闭调试输出:

<cfsetting showdebugoutput="No">