如何在javascript内部HTML中编写PHP代码,我想在包含HTML代码的javascript innerHTML中编写PHP代码
<?php
<script>
document.getElementById("view-img-path"+id).innerHTML ="<input type='hidden' name='new-image-attachment-id' id='image-attachment-id' value='"<?php echo get_option('media_selector_attachment_id'); ?>"'/>"
</script>
我想在里面编写PHP代码,我的代码是正确的,它在我的IDE中显示错误
答案 0 :(得分:1)
删除php标记之外的"
。同时在<?php
<script>
<script>
document.getElementById("view-img-path"+id).innerHTML ="<input type='hidden' name='new-image-attachment-id' id='image-attachment-id' value='<?php echo get_option('media_selector_attachment_id'); ?>'/>"
</script>
答案 1 :(得分:0)
我只创建一个
.php
文件,然后使用php代码Include
将该php文件包含在Js中。
<强> myfile.php 强>
<input type='hidden' name='new-image-attachment-id' id='image-attachment-id' value='<?php $myvalue = get_option('media_selector_attachment_id'); echo $myvalue; ?>
<强> HTML 强>
<script>
document.getElementById("view-img-path"+id).innerHTML ='<?php include_once "myfile.php";?>'
</script>
或(LiKe In Your Case)
<script>
document.getElementById("view-img-path"+id).innerHTML ="<input type='hidden' name='new-image-attachment-id' id='image-attachment-id' value='<?php $myvalue = get_option('media_selector_attachment_id'); echo $myvalue; ?>'/>"
</script
OR
var option = '<?php echo get_option('media_selector_attachment_id'); ?>';
document.getElementById("view-img-path"+id).innerHTML ="<input type='hidden' name='new-image-attachment-id' id='image-attachment-id' value='+ option +'/>"
答案 2 :(得分:0)
var option = '<?php echo get_option('media_selector_attachment_id'); ?>';
document.getElementById("view-img-path"+id).innerHTML ="<input type='hidden' name='new-image-attachment-id' id='image-attachment-id' value='+ option +'/>"
您的代码必须位于。 php 文件