如何使用jquery

时间:2016-02-08 15:52:02

标签: javascript jquery dom

当我点击fold2.1时,我需要获得值" fold2.1"和" fold2"当我点击" doc 6"我需要得到" doc6" ," fold2.1"和" fold2"值。

与" fold2.2"相同我需要获得价值观" fol2.2"和" fold2"当我点击chld" Doc 7"我需要得到#7; Doc 7" ," fol 2.2"和" fol2"值。

HTML文件:

<li class="favdelParentFold">
  <a href="#parentLevel1" data-toggle="collapse" class="" aria-expanded="true">
    <span class="caret-right"></span>
  </a><span class="folder"></span><span id="pfDel">fold2</span><span data-toggle="modal" data-target="#AddFavorites_ModalDel" class="trashcan"> press me</span>
  <ul class="collapse in" id="parentLevel1" aria-expanded="true">

    <li class="favorite"><a id="pfDocDel" href="">one 1</a><span data-toggle="modal" data-target="#AddFavorites_ModalDel" class="trashcan">press me</span></li>
    <li class="favorite"><a id="pfDocDel" href="">one 2</a><span data-toggle="modal" data-target="#AddFavorites_ModalDel" class="trashcan">press me</span></li>
    <li class="favorite"><a id="pfDocDel" href="">one 3</a><span data-toggle="modal" data-target="#AddFavorites_ModalDel" class="trashcan">press me</span></li>
    <!-- ROOT DOCS -->

    <li class="favdelSubFold">
      <a href="#1childlevel1" data-toggle="collapse">
        <span class="caret-right"></span></a><span class="folder">
                                                                    </span><span id="sfDel">fold2.1</span><span data-toggle="modal" data-target="#AddFavorites_ModalDel" class="trashcan">press me</span>
      <ul id="1childlevel1" class="collapse in">

        <li class="favorite">
          <a id="sfDocDel" href="https://www.cna.com" target="_blank">doc6</a><span data-toggle="modal" data-target="#AddFavorites_ModalDel" class="trashcan">press me</span></li>
      </ul>
    </li>
    <li class="favdelSubFold">
      <a href="#1childlevel2" data-toggle="collapse">
        <span class="caret-right"></span></a><span class="folder">
                                                                    </span><span id="sfDel">fold2.2</span><span data-toggle="modal" data-target="#AddFavorites_ModalDel" class="trashcan">press me</span>

      <ul id="1childlevel2" class="collapse in">

        <li class="favorite">
          <a id="sfDocDel" href="https://www.cna.com" target="_blank">doc7</a><span data-toggle="modal" data-target="#AddFavorites_ModalDel" class="trashcan">press me</span></li>

      </ul>

    </li>
  </ul>
</li>

JS脚本

$(".favdelParentFold ul li .trashcan").click(function() {
  if ($(this).parent().find('ul').length || $(this).closest('.favdelSubFold').length <1 ) return;

  var tr = $(this).prev('a').text();
  var pa = $(this).closest('.favdelParentFold').find('#pfDel').text();
  var ya = $(this).parent('#sfDocDel').text();
  alert(tr);
  alert(pa);
  alert(ya);
});

工作代码:

https://jsfiddle.net/atg5m6ym/409/

1 个答案:

答案 0 :(得分:0)

您不能在页面上拥有重复的ID。这是无效的HTML,浏览器只能访问第一个匹配项。对$sql = "INSERT IGNORE table SET user_id = :uid"; if ($con = $connection->prepare($sql)) { $con->execute([':uid' => intval($this->uid)]); } 值使用一个类(对于每个元素都不具有唯一ID的其他任何值)。

然后更改代码的几行以使用id=pfDel等的类选择器:

pfDel

更正样本: https://jsfiddle.net/TrueBlueAussie/atg5m6ym/413