如何获取父项的id和li项的位置

发布于 2024-08-06 00:30:40 字数 1744 浏览 3 评论 0原文

我想通过拖放来更改嵌套导航,我找到了一个不错的插件。现在我想将更改存储在数据库中,但不幸的是我无法读出父元素的 id。我需要的第二件事是拖动的 li 元素在列表

<script type="text/javascript" src="includes/jquery/jquery.js"></script>
<script type="text/javascript" src="includes/jquery/interface.js"></script>
<script type="text/javascript" src="includes/jquery/inestedsortable-1.0.pack.js"></script>
<script type="text/javascript">
    jQuery( function($) {
        $('#Hnav, #Nnav').NestedSortable(
            {
                accept: 'sort',
                noNestingClass: "no-children",
                helperclass: 'helper',
                autoScroll: true,
                onChange: function(serialized) {
            onStop : function(){
              $('#output').html($(this).id); // works fine
              $('#output').html($(this).parent().parent().att('id')); // fail
                  $('#output').html(this.closest('li').att('id')); // fail
            },
                nestingPxSpace : '0'
            }
        );
    });
</script>

html

<div id="sitemap">
<ul id="Hnav">
  <li id="n1"><a href="contentsite.php?cont=1">Home</a></li>
  <li id="n2" class="sort"><a href="contentsite.php?cont=2">Choir</a>
    <ul class="level2">
      <li id="n4" class="sort"><a href="contentsite.php?cont=3">lkff</a></li>
      <li id="n6" class="sort"><a href="contentsite.php?cont=5">changethis</a></li>
      <li id="n5" class="sort"><a href="contentsite.php?cont=4">History</a></li>
    </ul>
  </li>

中的新位置...

有什么想法吗?谢谢。

劳拉

I want to change a nested Navigation by drag and drop and i find a nice plugin. Now i want to store the changes in the database, but unfortually I can't read out the id of the parent element. The second thing what I need is the new position of the dragged li element in the list

<script type="text/javascript" src="includes/jquery/jquery.js"></script>
<script type="text/javascript" src="includes/jquery/interface.js"></script>
<script type="text/javascript" src="includes/jquery/inestedsortable-1.0.pack.js"></script>
<script type="text/javascript">
    jQuery( function($) {
        $('#Hnav, #Nnav').NestedSortable(
            {
                accept: 'sort',
                noNestingClass: "no-children",
                helperclass: 'helper',
                autoScroll: true,
                onChange: function(serialized) {
            onStop : function(){
              $('#output').html($(this).id); // works fine
              $('#output').html($(this).parent().parent().att('id')); // fail
                  $('#output').html(this.closest('li').att('id')); // fail
            },
                nestingPxSpace : '0'
            }
        );
    });
</script>

html

<div id="sitemap">
<ul id="Hnav">
  <li id="n1"><a href="contentsite.php?cont=1">Home</a></li>
  <li id="n2" class="sort"><a href="contentsite.php?cont=2">Choir</a>
    <ul class="level2">
      <li id="n4" class="sort"><a href="contentsite.php?cont=3">lkff</a></li>
      <li id="n6" class="sort"><a href="contentsite.php?cont=5">changethis</a></li>
      <li id="n5" class="sort"><a href="contentsite.php?cont=4">History</a></li>
    </ul>
  </li>

...

Any idea? Thank you.

Lara

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

你曾走过我的故事 2024-08-13 00:30:40

我的猜测是,如果您在其上调用 parent() ,则必须使用 $() 语法包装 this

$('#output').html(this.id); // works fine
$('#output').html($(this).parent().parent().attr('id'));
$('#output').html($(this).closest('li').attr('id'));

编辑:

我有更新了代码以响应您的评论。 jQuery 对象不支持 .id,它们必须使用 .attr('id')

My guess is you have to wrap this with the $() syntax if you're calling parent() on it:

$('#output').html(this.id); // works fine
$('#output').html($(this).parent().parent().attr('id'));
$('#output').html($(this).closest('li').attr('id'));

Edit:

I have updated the code in response to your comment. jQuery objects do not support .id, they must use .attr('id').

っ左 2024-08-13 00:30:40

您可以使用:

 $('#output').html($(this).parent().parent().attr('id')); // or
 $('#output').html($(this).closest('li').attr('id')); // or
 $('#output').html(this.parentNode.parentNode.id);

You could use either:

 $('#output').html($(this).parent().parent().attr('id')); // or
 $('#output').html($(this).closest('li').attr('id')); // or
 $('#output').html(this.parentNode.parentNode.id);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文