【问题标题】:jquery+on iterating loop how can i know which element is clickedjquery+on迭代循环我怎么知道点击了哪个元素
【发布时间】:2020-12-08 23:37:35
【问题描述】:

我有一个列表,它实际上是从 JSON 派生的国旗、国家名称和国家代码

我已经编写了 for 循环来呈现这样的 html 元素

data = 

  [
  {
        "name": "INDIA ",
        "code": "93",
        "url": 'https://www.countryflags.io/in/flat/64.png' 
    }, {
        "name": "JAPAN ",
        "code": "355",
        "url": 'https://www.countryflags.io/jp/flat/64.png'
    }]

for (var i = 0; i < data.length; i++) {
  
  var countryName = data[i].name;
  var countrtDialCode = data[i].code;
  var countryUrl = data[i].url;
  var badge = document.createElement('div');
  badge.className = 'badge';
  badge.innerHTML =
  '<div id="listSection">'+
    '<div style="display:flex">'+
    '<div id="flagSection">'+'<img style="height:10px;width:20px;margin-top:4px;" src='+countryUrl+'>'+'</div>'+'&nbsp;&nbsp;'+
    '<div id="nameSection">' + countryName + '</div>' +'&nbsp;&nbsp;'+
    '<div id="codeSection">' + countrtDialCode + '</div>' 
     +'</div>'+
     '</div>'
    
  document.getElementById('countries-list').appendChild(badge);
}

我也有一个 div 部分

<div id="inputSection"> </div>

<div id="countries-list">

</div>

我已经这样做了,当您单击输入部分时,列表将会出现,我可以从列表中进行选择,并且我需要 应该显示标志

<script type="text/javascript">
    $(document).ready(function(){
        $('#countries-list').addClass('hideSection').removeClass('showSection')
    });
    $('#inputSection').click(function(){
        $('#countries-list').addClass('showSection').removeClass('hideSection')
    })


    </script>

所以当我从列表中单击印度 JSON 时,如果我单击输入部分列表,我应该再次在 inputSection 中显示印度标志,如果我选择 NEPAL,印度标志应该替换为 NEPAL 标志。

有 2 个问题。第一个我无法在 INNERHTML 中编写点击函数来识别点击了哪个国家,第二个是如何检索标志部分并将其显示在 inputSection 中。

任何小提琴都会非常有帮助和感激

【问题讨论】:

  • 您在元素上重复 ID。从网络标准的角度来看,这是无效的 html。如果你想要一个重复的标识符,你应该使用类来代替
  • 请提供一些示例数据,以便更容易测试行为。
  • 与问题无关:您应该有一个showSection(或hideSection),另一个(隐藏/显示)是默认值 - 然后您只需要切换一个类
  • @hev1 添加了 json 虚拟数据
  • 主要是2个问题-如何将onclick写入innerHTML?因为我需要知道单击了哪个标志 - 然后用标志替换 inputSection 并循环重复[再次如果用户单击另一个标志,应该删除已经选择的标志并且应该看到新标志]。我重复一遍,只有标志应该是可见的

标签: javascript html jquery user-interface


【解决方案1】:

如果您只需要复制输入部分中的标志部分,那么这就是您所需要的:

$('.listSection').on('click', function() {
    $('#inputSection').html( $('.flagSection', this).clone() );
});

但是,您必须将 JS 中 HTML 中出现的每个 id 转换为 class,如下面的工作演示所示。 id 属性值应该是唯一的。

$(function() {
    const data = [{
          "name": "INDIA ",
          "code": "93",
          "url": 'https://www.countryflags.io/in/flat/64.png' 
      }, {
          "name": "JAPAN ",
          "code": "355",
          "url": 'https://www.countryflags.io/jp/flat/64.png'
      }];

    for (var i = 0; i < data.length; i++) {

      var countryName = data[i].name;
      var countrtDialCode = data[i].code;
      var countryUrl = data[i].url;
      var badge = document.createElement('div');
      badge.className = 'badge';
      badge.innerHTML =
      '<div class="listSection">'+
        '<div style="display:flex">'+
        '<div class="flagSection">'+'<img style="height:10px;width:20px;margin-top:4px;" src='+countryUrl+'>'+'</div>'+'&nbsp;&nbsp;'+
        '<div class="nameSection">' + countryName + '</div>' +'&nbsp;&nbsp;'+
        '<div class="codeSection">' + countrtDialCode + '</div>' 
         +'</div>'+
         '</div>'

      document.getElementById('countries-list').appendChild(badge);
    }
    
    $('.listSection').on('click', function() {
        console.log( {name: $('.nameSection',this).text(), code: $('.codeSection', this).text()} );
        $('#inputSection').html( $('.flagSection', this).clone() );
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<div id="inputSection"> </div>

<div id="countries-list">

</div>

注意

由于.html( jQueryObject ) 的文档中没有提到它甚至认为它在上面的演示中有效,我将提供一个使用.empty().append() 方法的替代方法:

$('#inputSection').empty().append( $('.flagSection', this).clone() );

【讨论】:

  • 不是说这是错的,而是.clone 不是给你一个对象,.html() 不是接受文本吗? $("#dest").html($("#src").html()) 也会这样吗?片段有效,所以我猜 jquery 处理它。
  • @peterKA 非常感谢。快速怀疑 listSection 点击​​功能,是否有控制台我点击的整个对象(如印度,93)
  • 试试console.log( {name: $('.nameSection',this).text(), code: $('.codeSection', this).text()} ); 或任何类似的;查看更新的演示。
  • 好点@freedomn-m!我将更新我的答案以提供文档支持的替代方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-25
相关资源
最近更新 更多