Using javascript var in jQuery .html
Very simple question:
Is it po开发者_运维问答ssible to display a var in .html, using jQuery?
var inscr = '<?php echo "test"; ?>';
$j('#myPlaceHolder7').html('<li class="button3">+inscr</li>');
Thanks!
Thanks everyone for the quick help!
Yup:
$j('#myPlaceHolder7').html('<li class="button3">' + inscr + '</li>');
Just concatenate your var with the enclosing strings, using the +
operator.
Yes, just use the +
operator to concatenate the strings.
var inscr = '<?php echo "test"; ?>';
$j('#myPlaceHolder7').html('<li class="button3">' + inscr + '</li>');
Yah, try this:
var inscr = '<?php echo "test"; ?>';
$j('#myPlaceHolder7').html('<li class="button3">' + inscr + '</li>');
Now if your var is not a string or simple type, you will need a util to searialize the var. Try JSON2's stringify. This will show your object as JSON.
<script>
var inscr = <?php echo json_encode("test"); ?>;
$j('#myPlaceHolder7').html('<li class="button3">'+inscr+'</li>');
</script>
Results in this source:
<script>
var inscr = "test";
$j('#myPlaceHolder7').html('<li class="button3">'+inscr+'</li>');
</script>
See json_encode()
精彩评论