获取当前图片的路径-jQuery
我正在使用 这个来自 Codrops 的照片库 显示图像。我添加了一个按钮来删除单击[并放大]的当前图像。但是,这不会删除单击的图像,但会删除相册中的第一张照片。
如何获取被点击的图像的图像路径[即当前图像]?
这是我添加到图库脚本中的 jQuery 代码:
var image = $('#photos').attr("alt");
/* If delete is called */
$('#adelete').click(function(){
//create post data
var postData = {
"image" : image
};
//make the call
$.ajax({
type: "POST",
url: "delete.php",
data: postData,
success: function(data){
alert("Image deleted successfully! " +image+data);
}
});
});
PHP:
<?php
$image = $_REQUEST['image'];
if (isset($image)) {
unlink($image);
echo "Success";
}
?>
'photos' 是图像的 ID。
echo "<img id='photos' src='/thumbs/$image' alt='$dirname/$image' />";
如果我在画廊代码中使用 var image = $('#wrapper img').attr("alt");
,它会给出 undefined
错误并且没有图像被删除。我假设这是因为放大的图像是在运行时加载的。
I am using this photo gallery from Codrops to display the images. I've added a button to delete the current image which is clicked [and enlarged]. However this doesn't delete the image which is clicked, but it deletes the first photo in the album.
How do I get the image path of the image which is clicked [i.e. current image]?
This is the jQuery code that I've added to the gallery script:
var image = $('#photos').attr("alt");
/* If delete is called */
$('#adelete').click(function(){
//create post data
var postData = {
"image" : image
};
//make the call
$.ajax({
type: "POST",
url: "delete.php",
data: postData,
success: function(data){
alert("Image deleted successfully! " +image+data);
}
});
});
PHP:
<?php
$image = $_REQUEST['image'];
if (isset($image)) {
unlink($image);
echo "Success";
}
?>
'photos' is the ID of the image.
echo "<img id='photos' src='/thumbs/$image' alt='$dirname/$image' />";
If I use var image = $('#wrapper img').attr("alt");
as in the gallery code, it gives an undefined
error and no images are deleted. I'm assuming this is because the enlarged image is loaded at runtime.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是您放置在页面上的图像与面板中加载的图像元素不同。您应该在内容区域中搜索与要删除的面板中的图像具有相同 URL 的匹配图像,然后获取其
alt
属性。 [您真的需要alt
还是您无法从src
中找出服务器上的目录是什么?如果是这样,那么您就不必进行第二次查找;你可以直接从面板图像传回 url。]The problem is that the image that you are putting on the page isn't the same image element that is loaded in the panel. You should search for the matching image in your content area that has the same url as the image in the panel that you want to delete, then get it's
alt
attribute. [Do you really needalt
or couldn't you figure out from thesrc
what the directory is on the server? If so, that would save you from having to do the second look up; you could just pass back the url from the panel image.]我不知道所有其他混乱,但要获取单击的图像的路径,只需执行以下操作:
I don't know about all that other mess, but to get the path of an image that was clicked just do this:
您需要将 var image = $('#photos').attr("alt"); 放入
.click(...)
中You need to put
var image = $('#photos').attr("alt");
inside your.click(...)