简体   繁体   English

如何使用jquery从html提取元素?

[英]How to extract elements from html using jquery?

I have a popover that lists various playlists a user has. 我有一个弹出窗口,其中列出了用户拥有的各种播放列表。 On click, I am trying to get the video_id associated with the video to be added to the appropriate playlist. 点击时,我试图将与视频关联的video_id添加到适当的播放列表中。 I'm not sure how to implement this; 我不确定如何实现; the user clicks on the link in the popover, the video_id (from the div #add) and the playlist selected (there are multiple playlists that the user generates names for but only one is selected) is supposed to be extracted. 如果用户单击弹出式窗口中的链接,则应提取video_id(来自div #add)和所选的播放列表(有多个播放列表,用户为其生成名称,但仅选择了一个播放列表)。 Any advice? 有什么建议吗?

    <script type="text/javascript">
   $(document).ready(function() {
       $("#muncher").click(function(event){
       event.preventDefault();
            $.ajax({
                 type:"POST",
                 url:"/edit_favorites/",
                 data: {
                        'video_add': $('#add').val(), // from form
                        'playlist_selected': $('#').val() //from form
                        },
                 success: function(){

                        $('#muncher').html("Added");


                    }
            });
            return false;
       });

    });
</script>   


<div id = 'muncher'>
    <div id = 'add'>12345</div>
    <a href = "#" id="munch" rel="popover" data-html="true" data-placement="right"  data-content= "<a href= '#'>Favorites</a><br>

                <a href = '#' class = 'Funny'>Funny</a><br>

                <a href = '#' class = 'test'>test</a><br>

                <a href = '#' class = 'test1'>test1</a><br>                     

    " data-original-title="Add to your plate">
            Munch
    </a>    
</div>

You can try using jquery data attribute to store the ID within the A tag. 您可以尝试使用jquery数据属性将ID存储在A标签内。

or instead you can try to get the previous sibling which is the DIV tag above the A tag that was clicked then use .text() to get its value 或者相反,您可以尝试获取先前的兄弟姐妹 ,即被单击的A标签上方的DIV标签,然后使用.text()获取其值

Also you should not have multiple elements with the same ID? 另外,您不应该有多个具有相同ID的元素吗? It sounds like you have lots of items with ID="ADD" ? 听起来您有很多ID =“ ADD”的商品?

In this case you may need to chage your code from 在这种情况下,您可能需要从

 $('#add').val(), 

to

 $('#add', $(this)).text(), 

In an HTML document, .html() can be used to get the contents of any element, http://api.jquery.com/html/ so give this a try: 在HTML文档中, .html()可用于获取任何元素http://api.jquery.com/html/的内容,因此请尝试一下:

$('#add').html()

Daveo is correct, you would need to make the id of your divs unique, one option to do that is to use the playlist id along with add. Daveo是正确的,您需要使div的ID唯一,一种选择是将播放列表ID与添加一起使用。 like id="add_12345" 就像id="add_12345"

Then, instead of a .click jQuery method, use an onclick inline in each of your add_[id variable] divs: 然后,在每个add_ [id变量] div中使用onclick内联而不是.click jQuery方法:

<div id="add_12345" onclick="addClick(this);">

The javascript function to get the id: 获取ID的JavaScript函数:

function addClick(element) {
    id = element.id.slice(element.id.indexOf("_") + 1);
    alert(id);
    //your ajax post code here
}

There are several ways to handle what you are trying to achieve, check out this very good stackoverflow Q&A for more on this topic: Getting the ID of the element that fired an event 有几种方法可以处理您要达到的目标,有关此主题的更多信息,请参见此非常出色的stackoverflow问答: 获取触发事件的元素的ID。

You should change your markup, 12.2.2 Nested links are illegal 您应该更改标记, 12.2.2嵌套链接是非法的

Little confused about what exactly sure what you're trying to do. 几乎对确切确定要执行的操作感到困惑。 Is each video a child of playlist? 每个视频都是播放列表的子级吗? If so, try passing this.id as a parameter in the onClick event... 如果是这样,请尝试将this.id作为参数传递给onClick事件...

...click="toPlaylist(this.id)"

function toPlaylist(clicked) {
  var tempVideo = clicked;
  // Get a parent node ID like this...
  var tempList = $(clicked).parentNode.id;
  // Do something else...
  nextAction(tempVideo, tempList);
}

PS: You may have to do something like this PS:您可能必须做这样的事情

  var tempVideo = '#'+clicked;    

...for the variables, I'm not sure sure. ...对于变量,我不确定。

Hope this was helpful. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM