-1

私のコードは以下の通りです:

<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

4 に答える 4

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

.html() ドキュメント

または、本当にそのようにしたい場合は、コアの innerHTML を使用します。

$("#mydiv").get(0).innerHTML = email_body;
于 2013-07-29T11:56:50.077 に答える
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
于 2013-07-29T11:56:57.637 に答える
1

ドキュメントを確認してください:

$("#mydiv").html(email_body );
于 2013-07-29T11:56:39.733 に答える
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 );
},

考えてみてください。

于 2013-07-29T12:00:07.130 に答える