简体   繁体   English

在php页面中由ajax请求添加的jQuery不起作用

[英]Jquery added by ajax request in php page doesn't work

i have a script jquery that upload an image and show it by a php script called in this way: 我有一个脚本jQuery,可以上传图像并通过以这种方式调用的PHP脚本显示它:

<script type="text/javascript" >
 $(document).ready(function() { 
 $('#photoimg').live('change', function(){ 
   $("#imageform").ajaxForm({target:'#logo'}).submit();
});
}); 
</script>

where photoimg is the input type file and image form the form. 其中photoimg是输入类型文件,图像形成表单。 logo is the div where image will be. 徽标是图像将在的div。

Now when image is loaded in div whit php script in this way: 现在,当以这种方式将图像加载到div whit php脚本中时:

echo "<img src='".$actual_image_name."' id='imm' style=\"width:500;height:350;\" \>\n";

i have to add a jquery script to drag image inside the div to "center" it. 我必须添加一个jquery脚本以将div中的图像拖动到“中心”位置。 if i use echo after the line before in this way: 如果我以这种方式在行之前使用echo:

echo '<script type="text/javascript">'; 
echo "$(function(){";
echo "var maskWidth  = $(\"#logo\").width();";
echo "var maskHeight = $(\"#logo\").height();";
echo "var imgPos = $(\"#imm\").offset();";
echo "var imgWidth = $(\"#imm\").width();";
echo "var imgHeight  = $(\"#imm\").height();";
echo "var x1 = (imgPos.left + maskWidth) - imgWidth;";
echo "var y1 = (imgPos.top + maskHeight) - imgHeight;";
echo "var x2 = imgPos.left;";
echo "var y2 = imgPos.top;";
echo "$(\"#imm\").css({top: 0, left: 0, cursor: 'move'});";
echo "$(\"#imm\").draggable({ containment: [x1,y1,x2,y2] });";
echo "});\n";
echo "</script>\n";

it doesn't work. 它不起作用。 can you help me pls? 你能帮我吗?

You should never want to execute Javascript from an external source like this. 您永远都不想从这样的外部源执行Javascript。 Just put the code into the calling page/script and execute it after the Ajax content has been added. 只需将代码放入调用页面/脚本中,然后在添加Ajax内容后执行即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM