开发者

Using javascript var in jQuery .html

开发者 https://www.devze.com 2023-04-13 08:06 出处:网络
Very simple question: Is it po开发者_运维问答ssible to display a var in .html, using jQuery? var inscr = \'<?php echo \"test\"; ?>\';

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()

0

精彩评论

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

关注公众号