如何替换<div>标签的内部HTML?</div>

时间:2013-07-29 11:55:18

标签: javascript jquery html

我的代码如下:

<td style="background-color:#ffffff; padding:40px 22px; font-family:Arial, Helvetica, sans-serif; color:#505050; font-size:12px; font-weight:normal;" id='mydiv'>

        [[NEWSLETTER_BODY]]

        </td>

<input type="button" name="btn_preview" id="btn_preview" value="{$preview_value}" class="submit" id="preview_newsletter" onclick="get_text_area_value()">

<script language="javascript" type="text/javascript">
function get_text_area_value() {    
  var email_body = jQuery("textarea#newsletter_email_body").val();
  //alert(email_body);
  $("#mydiv").html ='';
  $("#mydiv").html = email_body;
}
</script>

我没有在id“mydiv”中获取新值,但是当我在警告中打印它时,它会显示在那里。在这方面你能帮助我取代div的价值吗?提前谢谢。

4 个答案:

答案 0 :(得分:2)

$("#mydiv").html(email_body);

.html() Documentation

或者如果你真的想这样做,那就是核心innerHTML:

$("#mydiv").get(0).innerHTML = email_body;

答案 1 :(得分:2)

添加html内容

$("#mydiv").html('html here');

仅用于添加text

$("#mydiv").text('text here');

$(element).html()将字符串视为HTML,$(element).text()将内容视为文字。

对于传递变量,您可以使用

$("#mydiv").html(var_name);
$("#mydiv").html(email_body); //in your case

但如果您只将文字传递给.text()

,我建议#mydiv
$("#mydiv").text(var_name);
$("#mydiv").text(email_body); //in your case

答案 2 :(得分:1)

检查DOC:

$("#mydiv").html(email_body );

答案 3 :(得分:0)

作为旁注,这是jQuery每次调用$()时所做的事情:

constructor: jQuery,
init: function( selector, context, rootjQuery ) {
var match, elem;

// HANDLE: $(""), $(null), $(undefined), $(false)
if ( !selector ) {
    return this;
}

// Handle HTML strings
if ( typeof selector === "string" ) {
    if ( selector.charAt(0) === "<" && selector.charAt( selector.length - 1 ) === ">" && selector.length >= 3 ) {
        // Assume that strings that start and end with <> are HTML and skip the regex check
        match = [ null, selector, null ];

    } else {
        match = rquickExpr.exec( selector );
    }

    // Match html or make sure no context is specified for #id
    if ( match && (match[1] || !context) ) {

        // HANDLE: $(html) -> $(array)
        if ( match[1] ) {
            context = context instanceof jQuery ? context[0] : context;

            // scripts is true for back-compat
            jQuery.merge( this, jQuery.parseHTML(
                match[1],
                context && context.nodeType ? context.ownerDocument || context : document,
                true
            ) );

            // HANDLE: $(html, props)
            if ( rsingleTag.test( match[1] ) && jQuery.isPlainObject( context ) ) {
                for ( match in context ) {
                    // Properties of context are called as methods if possible
                    if ( jQuery.isFunction( this[ match ] ) ) {
                        this[ match ]( context[ match ] );

                    // ...and otherwise set as attributes
                    } else {
                        this.attr( match, context[ match ] );
                    }
                }
            }

            return this;

        // HANDLE: $(#id)
        } else {
            elem = document.getElementById( match[2] );

            // Check parentNode to catch when Blackberry 4.6 returns
            // nodes that are no longer in the document #6963
            if ( elem && elem.parentNode ) {
                // Handle the case where IE and Opera return items
                // by name instead of ID
                if ( elem.id !== match[2] ) {
                    return rootjQuery.find( selector );
                }

                // Otherwise, we inject the element directly into the jQuery object
                this.length = 1;
                this[0] = elem;
            }

            this.context = document;
            this.selector = selector;
            return this;
        }

    // HANDLE: $(expr, $(...))
    } else if ( !context || context.jquery ) {
        return ( context || rootjQuery ).find( selector );

    // HANDLE: $(expr, context)
    // (which is just equivalent to: $(context).find(expr)
    } else {
        return this.constructor( context ).find( selector );
    }

// HANDLE: $(DOMElement)
} else if ( selector.nodeType ) {
    this.context = this[0] = selector;
    this.length = 1;
    return this;

// HANDLE: $(function)
// Shortcut for document ready
} else if ( jQuery.isFunction( selector ) ) {
    return rootjQuery.ready( selector );
}

if ( selector.selector !== undefined ) {
    this.selector = selector.selector;
    this.context = selector.context;
}

return jQuery.makeArray( selector, this );
},

想一想。