I'm trying to append an image to div with jQuery, this is my code:
我正在尝试使用jQuery将图像附加到div,这是我的代码:
$img = '<img src="my-correct-link.png" />';
$js = '<script type="text/javascript">
jQuery(document).ready(function() {
jQuery(".wonderpluginslider").append(' . $img .');
});
</script>';
echo $js;
The image not appears, if I replace the value of $img with simple $img = 'hello';
, hello is displayed on the screen.
如果我用简单的$ img ='hello'替换$ img的值,则不会出现图像;屏幕上会显示hello。
1 个解决方案
#1
You don't have quotes around your image HTML, the call to append requires a string, you should be getting a JavaScript syntax error.
你的图像HTML周围没有引号,对append的调用需要一个字符串,你应该得到一个JavaScript语法错误。
Your HTML output now looks like
您的HTML输出现在看起来像
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery(".wonderpluginslider").append(<img src="my-correct-link.png" />);
});
</script>;
But needs to be
但需要
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery(".wonderpluginslider").append('<img src="my-correct-link.png" />');
});
</script>;
The safest way is to use json_encocde
最安全的方法是使用json_encocde
$img = '<img src="my-correct-link.png" />';
$js = '<script type="text/javascript">
jQuery(document).ready(function() {
jQuery(".wonderpluginslider").append(' . json_encode($img).');
});
</script>';
echo $js;
#1
You don't have quotes around your image HTML, the call to append requires a string, you should be getting a JavaScript syntax error.
你的图像HTML周围没有引号,对append的调用需要一个字符串,你应该得到一个JavaScript语法错误。
Your HTML output now looks like
您的HTML输出现在看起来像
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery(".wonderpluginslider").append(<img src="my-correct-link.png" />);
});
</script>;
But needs to be
但需要
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery(".wonderpluginslider").append('<img src="my-correct-link.png" />');
});
</script>;
The safest way is to use json_encocde
最安全的方法是使用json_encocde
$img = '<img src="my-correct-link.png" />';
$js = '<script type="text/javascript">
jQuery(document).ready(function() {
jQuery(".wonderpluginslider").append(' . json_encode($img).');
});
</script>';
echo $js;