好的,这是我的代码:
//Other Jquery codes
// show_popup_crop : show the crop popup
function show_popup_crop(url) {
alert("Called show_popup_crop!");
// change the photo source
$('#cropbox').attr('src', url);
// destroy the Jcrop object to create a new one
try {
jcrop_api.destroy();
} catch (e) {
// object not defined
}
// Initialize the Jcrop using the TARGET_W and TARGET_H that initialized before
$('#cropbox').Jcrop({
aspectRatio: 1,
setSelect: [ 100, 100, TARGET_W, TARGET_H ],
onSelect: updateCoords
},function(){
jcrop_api = this;
});
// store the current uploaded photo url in a hidden input to use it later
$('#photo_url').val(url);
// hide and reset the upload popup
$('#display_pic_input_first').val('');
// show the crop popup
$('#popup_crop').show();
}
// crop_photo :
function crop_photo() {
var x_ = $('#x').val();
var y_ = $('#y').val();
var w_ = $('#w').val();
var h_ = $('#h').val();
var photo_url_ = $('#photo_url').val();
// hide thecrop popup
$('#popup_crop').hide();
// display the loading texte
$('.loading').css('display','inherit');
// crop photo with a php file using ajax call
$.ajax({
url: 'crop_photo.php',
type: 'POST',
data: {x:x_, y:y_, w:w_, h:h_, photo_url:photo_url_, targ_w:TARGET_W, targ_h:TARGET_H},
success: function(data){
// display the croped photo
}
});
}
// updateCoords : updates hidden input values after every crop selection
function updateCoords(c) {
$('#x').val(c.x);
$('#y').val(c.y);
$('#w').val(c.w);
$('#h').val(c.h);
}
//document.ready function calls
怎么了:
show_popup_crop(url){}
不起作用。alert("Called show _popup_crop!");
正在执行。但是,它并不需要执行其余的代码,而这是需要更改图像标签和分隔的属性的部分。
PHP代码自动生成包含在主体中的脚本:
<script type="text/javascript">window.top.window.show_popup_crop("some url")</script>
php页面中script标记内的PHP代码:
echo '<script type="text/javascript">window.top.window.show_popup_crop("'.$target_file.'")</script>';
alert("Called show_popup_crop!");
执行警报功能时,页面加载时确实会调用该功能。实际上,它执行包含的所有警报功能,但不执行方法中包含的其他任何功能。我尝试删除所有其他内容并仅执行下面的代码,但仍然无法正常工作:
function show_popup_crop(url) {
alert("Called show_popup_crop!");
$('#cropbox').attr('src', url); //change the photo source
$('#popup_crop').show();
}
$('#cropbox').attr('src', "some url");
在document.ready
同一个JS文件中包含另一个方法时,它会执行代码。我不明白这是什么问题。在index.php页面中调用的所有其他函数都将执行。
然而,还有一个奇怪的事情是,它没有执行中的所有功能$(document).ready(function(){...});
。
它只是执行第一个功能并停止...但是它执行所有其他页面中的所有其他功能?
控制台错误:
起初它给了我这个控制台错误:
未捕获的TypeError:undefined不是函数
但是现在它给了我这个:
未捕获的ReferenceError:未定义jQuery
标头中包含的文件(其中position.js是文件名):
<script type="text/javascript" src="js/jquery.min.js"></script>
<script src="js/jquery.Jcrop.min.js"></script>
<script src="js/position.js"></script>
PS:经常在刷新chrome和更改js文档时,无论刷新多少次,chrome都会加载旧的js文档?
将您的PHP代码更改为此:
echo '<script type="text/javascript">$(window).on("load",function(){window.top.window.show_popup_crop("'.$target_file.'");});</script>';
JavaScript中的结果将是这样的:
<script type="text/javascript">
$(window).on("load",function(){
window.top.window.show_popup_crop("url/to/image.jpg");
});
</script>
show_popup_crop()
是在图像元素#cropbox
完全加载到页面上之前被调用的。$('#cropbox').attr('src', url);
尝试更改src
尚不存在的元素的标签。要做的$(window).on("load",function(){...});
是,等到页面上的所有元素都完全加载后,再执行其处理函数中的代码。
这发生在之后document.ready
。在此阅读两者之间的区别是什么,以及为什么window.load
特别是将其用于图像很重要(我总是建议在document.ready上使用它)。
因此,现在$('#cropbox').attr('src', url);
仅在将所有元素(包括图像)加载到页面上之后才执行此行。因此,存在图像元素,并且可以成功更改源。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句