当我在jquery中单击该图像时如何获取图像的源

时间:2022-11-09 20:34:18

I want to get the source of an image when i clicked inside li element. please guide me on this.

我想在li元素中单击时获取图像的来源。请指导我这个。

<li class="k-tile" data-type="f" data-uid="504c365a-fd54-4a9f-adc2-c217ce4dc8fc" 
                                               role="option" aria-selected="false">
    <div class="k-thumb">
        <img class="k-image" alt="1.jpg" style="" 
                           src="http://doelbewust.nl/img/doelbewust_logo.png">
    </div>
    <strong>1.jpg</strong>
    <span class="k-filesize">206.97 KB</span>
</li>

5 个解决方案

#1


Find the image of the clicked li and use the attr() function:

找到被点击的li的图像并使用attr()函数:

$('li.k-tile').on('click', function() {
    var that = $(this);
    var src = that.find('img').attr('src');
    alert(src);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<li class="k-tile" data-type="f" data-uid="504c365a-fd54-4a9f-adc2-c217ce4dc8fc" role="option" aria-selected="false">
    <div class="k-thumb">
        <img class="k-image" alt="1.jpg" style="" src="http://doelbewust.nl/img/doelbewust_logo.png">
    </div>
    <strong>1.jpg</strong>
    <span class="k-filesize">206.97 KB</span>
</li>

#2


I hope I have understood you correctly

我希望我能正确理解你

   $("img.k-image").click(function() {
    var imgSrc = $(this).attr("src");
    })

#3


Assuming you mean that you want to hook the event to the click of the li, you would need to use find() to retrieve the related img element and its src. Try this:

假设你想要将事件挂钩到li的点击,你需要使用find()来检索相关的img元素及其src。试试这个:

$('.k-tile').click(function() {
    var src = $(this).find('.k-image').prop('src');
    // do something with src...
});

#4


    <li class="k-tile" data-type="f" data-uid="504c365a-fd54-4a9f-adc2-c217ce4dc8fc" role="option" aria-selected="false">
        <div class="k-thumb">
            <img class="k-image" alt="1.jpg" style="" src="http://doelbewust.nl/img/doelbewust_logo.png">
        </div>
        <strong>1.jpg</strong>
        <span class="k-filesize">206.97 KB</span>
    </li>

    $(".k-thumb>img.k-image").click(function() {
        var src = $(this).attr("src");
        alert(src);
    });

http://jsfiddle.net/d2mc0xzq/

#5


you can use attr function in jquery and bind click event

你可以在jquery中使用attr函数并绑定click事件

$('img').on({
'click': function() {
     var src =  $(this).attr('src');
 }
});

#1


Find the image of the clicked li and use the attr() function:

找到被点击的li的图像并使用attr()函数:

$('li.k-tile').on('click', function() {
    var that = $(this);
    var src = that.find('img').attr('src');
    alert(src);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<li class="k-tile" data-type="f" data-uid="504c365a-fd54-4a9f-adc2-c217ce4dc8fc" role="option" aria-selected="false">
    <div class="k-thumb">
        <img class="k-image" alt="1.jpg" style="" src="http://doelbewust.nl/img/doelbewust_logo.png">
    </div>
    <strong>1.jpg</strong>
    <span class="k-filesize">206.97 KB</span>
</li>

#2


I hope I have understood you correctly

我希望我能正确理解你

   $("img.k-image").click(function() {
    var imgSrc = $(this).attr("src");
    })

#3


Assuming you mean that you want to hook the event to the click of the li, you would need to use find() to retrieve the related img element and its src. Try this:

假设你想要将事件挂钩到li的点击,你需要使用find()来检索相关的img元素及其src。试试这个:

$('.k-tile').click(function() {
    var src = $(this).find('.k-image').prop('src');
    // do something with src...
});

#4


    <li class="k-tile" data-type="f" data-uid="504c365a-fd54-4a9f-adc2-c217ce4dc8fc" role="option" aria-selected="false">
        <div class="k-thumb">
            <img class="k-image" alt="1.jpg" style="" src="http://doelbewust.nl/img/doelbewust_logo.png">
        </div>
        <strong>1.jpg</strong>
        <span class="k-filesize">206.97 KB</span>
    </li>

    $(".k-thumb>img.k-image").click(function() {
        var src = $(this).attr("src");
        alert(src);
    });

http://jsfiddle.net/d2mc0xzq/

#5


you can use attr function in jquery and bind click event

你可以在jquery中使用attr函数并绑定click事件

$('img').on({
'click': function() {
     var src =  $(this).attr('src');
 }
});