获取当前图片的路径-jQuery

发布于 2024-11-05 10:31:29 字数 1375 浏览 4 评论 0原文

我正在使用 这个来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神经大条 2024-11-12 10:31:29

问题是您放置在页面上的图像与面板中加载的图像元素不同。您应该在内容区域中搜索与要删除的面板中的图像具有相同 URL 的匹配图像,然后获取其 alt 属性。 [您真的需要 alt 还是您无法从 src 中找出服务器上的目录是什么?如果是这样,那么您就不必进行第二次查找;你可以直接从面板图像传回 url。]

/* If delete is called */
$('#adelete').click(function(){
    var src = $('#wrapper img').attr('src');

    var image = $('#content img:has([src="' + src + '"])').attr('alt');

    //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);  
        }
    });
});

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 need alt or couldn't you figure out from the src 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.]

/* If delete is called */
$('#adelete').click(function(){
    var src = $('#wrapper img').attr('src');

    var image = $('#content img:has([src="' + src + '"])').attr('alt');

    //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);  
        }
    });
});
厌倦 2024-11-12 10:31:29

我不知道所有其他混乱,但要获取单击的图像的路径,只需执行以下操作:

$('#photos').click(function() {
  var path = $(this).attr('src');
});

I don't know about all that other mess, but to get the path of an image that was clicked just do this:

$('#photos').click(function() {
  var path = $(this).attr('src');
});
遗忘曾经 2024-11-12 10:31:29

您需要将 var image = $('#photos').attr("alt"); 放入 .click(...)

You need to put var image = $('#photos').attr("alt"); inside your .click(...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文