Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在列表中查找类的值_Javascript_Jquery - Fatal编程技术网

Javascript 在列表中查找类的值

Javascript 在列表中查找类的值,javascript,jquery,Javascript,Jquery,我有 但这会得到两个loc id的值。我只想要我正在选择的那个(单击) 有关完整内容,请参见此处: 感谢var targetID=$(此).find('.loc id').text()若要工作,您必须单击仅为一个.loc id的上升元素。例如: $('#list').click(function () { //Change the src of img var targetID = $(this).find('.loc-id').text(); // Get the ID

我有

但这会得到两个loc id的值。我只想要我正在选择的那个(单击)

有关完整内容,请参见此处:

感谢
var targetID=$(此).find('.loc id').text()
若要工作,您必须单击仅为一个
.loc id
的上升元素。例如:

 $('#list').click(function () {
    //Change the src of img
    var targetID = $(this).find('.loc-id').text();  // Get the ID

    // Since array of objects isn't indexed, need to loop to find the correct one
    var foundObject = null;
    for (var key in parsedArray) {
        if (parsedArray.hasOwnProperty(key) && parsedArray[key].id == targetID) {
            foundObject = parsedArray[key];
            break;
        }
    }

    // If the object is found, extract the image and set!
    if (!foundObject)
        return;

    var imageSrc = foundObject.LocationPhoto; // From the object
    $('#location-image').attr('src', imageSrc); // Set the new source
});

在单击处理程序中,
引用具有多个子元素的
元素

将单击处理程序改为充当委托:

$('.list-details').on('click',function(){
    var targetID = $(this).find('.loc-id').text();
});

现在,在单击处理程序中,
这个
引用一个
  • 元素,因此搜索应该只产生一个值。

    您需要更改选择器。在事件处理程序中
    $(此)
    指具有多个
    loc id
    ul
    ,因此当您使用
    text()
    其连接文本时

    使用

    而不是

    $('#list li').click(function () {
        //Change the src of img
        var targetID = $(this).find('.loc-id').text(); // Get the ID
        alert('targetID: ' + targetID)
    });
    

    如果您在
    #列表
    上授权,则可以使用
    event.target

    演示:


    如何使用
    var targetID=$(this.find('.loc id').text()?显示完整的代码。您在哪里单击?@Satpal查看原始问题的完整上下文。
    $(此)
    指的是
    ul
    ,并且有多个
    。loc id
    。您说:“这获取两个loc id的值”。但是你的选择器使用的是类而不是ID,所以如果($this)代表你的列表,那么得到2个结果是正常的,这真的很好,谢谢,但是我有一个问题,列表li触发了一个事件,但逻辑上它是在球上的。
    $('.list-details').on('click',function(){
        var targetID = $(this).find('.loc-id').text();
    });
    
    $('#list').on('click', 'li', function () {
    
    $('#list li').click(function () {
        //Change the src of img
        var targetID = $(this).find('.loc-id').text(); // Get the ID
        alert('targetID: ' + targetID)
    });
    
    // When we select the item
    $('#list').click(function () {
        //Change the src of img
        var targetID = $(this).find('.loc-id').text();  // Get the ID
    
    $("#list").on("click", function(e) {
        var $t = $(e.target);
        if ($t.hasClass('loc-id')) {
            alert($t.text());
        }
    });