最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - setting innerHTML for all browsers in javascript - Stack Overflow

programmeradmin6浏览0评论

I want to load contents of a DIV dynamically in javascript.I used this code

 var strHtml="<h3>Test</h3>";
 var div = $("#divPrice");  
 div.innerHTML=strHtml

This works in IE. But not in firefox.Whats the alternative of this which works on all browsers ?

I want to load contents of a DIV dynamically in javascript.I used this code

 var strHtml="<h3>Test</h3>";
 var div = $("#divPrice");  
 div.innerHTML=strHtml

This works in IE. But not in firefox.Whats the alternative of this which works on all browsers ?

Share Improve this question edited Jun 7, 2009 at 9:50 Paolo Bergantino 488k82 gold badges521 silver badges437 bronze badges asked Jun 7, 2009 at 9:44 ShyjuShyju 219k106 gold badges419 silver badges498 bronze badges 1
  • 4 I'm surprised this works in IE... It certainly shouldn't – James Commented Jun 7, 2009 at 10:49
Add a ment  | 

4 Answers 4

Reset to default 8

Try it this way:

var strHtml="<h3>Test</h3>";
$("#divPrice").html(strHtml);

It looks like you are using jquery, so you can use:

 var strHtml="<h3>Test</h3>";
 var div = $("#divPrice");  
 div.html(strHtml);

I take it you're using a JavaScript Framework based on $(). Looking at your other questions, it looks like you're using jQuery, in which case you can do

$("#divPrice").html(strHtml);

Just for reference, jQuery's html() mand does the following

    jQuery.fn = jQuery.prototype = {
        html: function( value ) {
            return value === undefined ?
                (this[0] ?
                    this[0].innerHTML.replace(/ jQuery\d+="(?:\d+|null)"/g, "") :
                    null) :
                this.empty().append( value );
        }
    }

I assume you use pure javascript, not jquery.

var div = $("#divPrice");

should be

var div = document.getElementById("divPrice");

Others are fine.

发布评论

评论列表(0)

  1. 暂无评论