开发者

Change the content of a div in IE8 using javascript

开发者 https://www.devze.com 2023-03-13 19:20 出处:网络
I am trying to change the content of the div using javascript/jquery. While the code works on IE9, FF, Chrome, Safari and Opera. It fails on IE 8. I have already tried .append, .appendTo, .innerHTML.

I am trying to change the content of the div using javascript/jquery. While the code works on IE9, FF, Chrome, Safari and Opera. It fails on IE 8. I have already tried .append, .appendTo, .innerHTML. The content of the container div is always empt开发者_JS百科y

What could be wrong. The code is

<script type="text/javascript">
jQuery(document).ready(
function()
{
var containers = jQuery('.page_content');


if (!containers.length)
{
return
}
var container = containers.eq(0);

container.block();
jQuery.get("<?php echo Router::url( array('controller' => 'user',
                        'action' => 'profile',
                        'admin' => false));
 ?>
    ", function(data)
    {
    if (jQuery.browser.msie)
    {

        alert(container.html()); //returns correct result
        container.empty();
        container.innerHTML = new String(data); 
        alert(data); //shows right output, some html
        alert(container.html()); //is always empty
    }
    else
    {
        container.html(data);
    }
    container.unblock();
    }
    );

    }

    );
</script>


Use replaceWith:

$(container).replaceWith(data);

That is, if data is the same div content as of the original.

Note: I am using IE8 to test.


If you simply want to reset the data within the container, you could do this:

container.html(data);

Check out what happens when you pass a string to .html( htmlString )

Perhaps your selector is not working properly. If you only want the first element with the .page_content class, why not do:

var container = jQuery('.page_content:first');


I have just resolved the issue by using the following code

error: function (data) {
    var resultedHTML = data.responseText;
    $('#Import-1').html(resultedHTML);
    return false;
}

response was not working for me. Hence i resolved it using responseText

0

精彩评论

暂无评论...
验证码 换一张
取 消