使用jquery insertAfter在html内部使用php变量
我有一个类似这样的php函数使用jquery insertAfter在html内部使用php变量,php,jquery,html,wordpress,Php,Jquery,Html,Wordpress,我有一个类似这样的php函数 function get_some_info() { $location = other_function_that_returns_a_string(); return <<<HTML <script> $("<h1> <?php echo $location ?> </h1>").insertAfter("header"); </script> HTML; } 函数获取一些
function get_some_info()
{
$location = other_function_that_returns_a_string();
return <<<HTML
<script>
$("<h1> <?php echo $location ?> </h1>").insertAfter("header");
</script>
HTML;
}
函数获取一些信息()
{
$location=返回字符串()的其他函数;
return问题在于您使用了语法
您不必在其中使用
这很好:
$("<h1> $location </h1>").insertAfter("header");
$(“$location”)。插入后面(“标题”);
以下是我测试的代码:
标题
如果您硬编码$位置
?比如$(“test”).insertAfter(“header”);
-我怀疑jquery选择器会找到类似的东西。还有
是的,那么近,那么远。一旦我们得到了herdoc的怪癖,一切都会顺利进行。这是我需要看到的
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header>
Header
</header>
<?php
function other_function_that_returns_a_string(){
return "A string";
}
function get_some_info(){
$location = other_function_that_returns_a_string(); // Will be "A string"
return <<<HTML
<script>
$("<h1> $location </h1>").insertAfter("header");
</script>
HTML;
}
echo get_some_info();
?>