简体   繁体   English

使用jQuery,有没有一种创建onclick函数的方法,该函数会将链接从一个UL传递到另一个UL?

[英]Using jQuery, is there a way to create an onclick function that will pass a link from one UL to another?

For instance I have a function that creates a list of links and would like the ability to have it where upon click of one of the populated links it passes part of the link to another list. 例如,我有一个创建链接列表的函数,并且希望能够在单击其中一个填充的链接时将链接的一部分传递到另一个列表。

Is there a way to do this? 有没有办法做到这一点?

Thank you for your time, I hope this question wasn't to vague. 谢谢您的宝贵时间,希望这个问题不要含糊。

This is roughly what the list creating function looks like: 列表创建函数大致如下所示:

$.getJSON("https://api.twitch.tv/kraken/search/streams?q=starcraft&limit=30&&type=top&callback=?", function (data) {
    var temp = "";

    $.each(data.streams, function (index, item) {
        temp = temp + "<li><a target='iframe1' href='http://www.twitch.tv/widgets/live_embed_player.swf?channel=" + item.channel.name + "'>" + item.channel.display_name + "</a></li>";

    });

    $("#list ul ").html(temp);
});

its no clear what you want to pass. 还不清楚您想通过什么。

You can use custom data using data-* attributes to pass data. 您可以使用使用data- *属性的自定义数据来传递数据。

$.getJSON("https://api.twitch.tv/kraken/search/streams?q=starcraft&limit=30&&type=top&callback=?", function (data) {
    var temp = "";
    $.each(data.streams, function (index, item) {
        temp = temp 
        + "<li><a target='iframe1'"
        + " data-channel-name='" + item.channel.name +"'" 
        +" href='http://www.twitch.tv/widgets/live_embed_player.swf?channel=" 
        + item.channel.name + "'>" 
        + item.channel.display_name + "</a></li>";
    });
    $("#list ul ").html(temp);
});

As you are creating elements dynamically. 在动态创建元素时。

You need to use Event Delegation . 您需要使用事件委托 You have to use .on() using delegated-events approach. 您必须使用.on()使用委托事件方法。

$('#list ul').on('click', "li a[target='iframe1']", function (event) {
    //To prevent default event
    event.preventDefault();
    var href = $(this).attr('href');
    var channelName = $(this).data('channel-name');
})

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. 委派事件的优势在于,它们可以处理来自后代元素的事件,这些事件在以后的时间添加到文档中。

Here is an example of what you can do, to start from: 这是您可以做的一个示例,从以下开始:

//de Setup a function to pull in AJAX information and append it to a list.
function exampleAppend() {
    $.getJSON("https://api.twitch.tv/kraken/search/streams?q=starcraft&limit=30&&type=top&callback=?", function(data) {
        var temp = "";

        $.each(data.streams, function(index, item) {
            $("#list ul ").append(
                "<li><a target='iframe1' " +
                "href='http://www.twitch.tv/widgets/live_embed_player.swf?channel=" +
                item.channel.name + "'>" +
                item.channel.display_name +
                "</a></li>"
            )
        });
    });
}

//de Setup your on-click handler.
$(document).ready(function() {
        $("#some_element").click( exampleAppend )
})

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

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