Javascript jquery onclick不工作

Javascript jquery onclick不工作,javascript,jquery,hyperlink,onclick,Javascript,Jquery,Hyperlink,Onclick,我正在动态创建一个链接,并尝试向其添加一个单击功能。正在添加链接,但单击功能不起作用。我在控制台中看不到任何东西,也没有收到警报 var section = $('<section></section>').append('<a class="link" href="#"></a>'); section.find('a.link').attr('title', post.data.permalink) .

我正在动态创建一个链接,并尝试向其添加一个单击功能。正在添加链接,但单击功能不起作用。我在控制台中看不到任何东西,也没有收到警报

var section = $('<section></section>').append('<a class="link" href="#"></a>');
section.find('a.link').attr('title', post.data.permalink)
                      .text(post.data.title)
                      .click(function()
                      {
                           console.log("function");
                           alert("hi");
                           getThread(post.data.permalink);
                       });
items.push(section[0].outerHTML);
$('#posts').empty().append(items.join(''));
var节=$('').append('');
section.find('a.link').attr('title',post.data.permalink)
.text(post.data.title)
。单击(函数()
{
console.log(“函数”);
警报(“hi”);
getThread(post.data.permalink);
});
items.push(节[0].outerHTML);
$('#posts').empty().append(items.join('');

jQuery最常见的错误之一。动态添加元素时,普通jQuery事件处理程序不起作用,因此需要使用才能将事件绑定到动态元素。这应该起作用:

var section = $('<section></section>').append('<a class="link" href="#"></a>');
section.find('a.link').attr('title', post.data.permalink)
                      .text(post.data.title)
                      .live("click", function()
                      {
                           console.log("function");
                           alert("hi");
                           getThread(post.data.permalink);
                       });
items.push(section[0].outerHTML);
$('#posts').empty().append(items.join(''));
var节=$('').append('');
section.find('a.link').attr('title',post.data.permalink)
.text(post.data.title)
.live(“单击”,函数()
{
console.log(“函数”);
警报(“hi”);
getThread(post.data.permalink);
});
items.push(节[0].outerHTML);
$('#posts').empty().append(items.join('');

注意
.live(“click”,function(){…})的用法那里?这应该可以解决您的问题。

jQuery最常见的错误之一。动态添加元素时,普通jQuery事件处理程序不起作用,因此需要使用才能将事件绑定到动态元素。这应该起作用:

var section = $('<section></section>').append('<a class="link" href="#"></a>');
section.find('a.link').attr('title', post.data.permalink)
                      .text(post.data.title)
                      .live("click", function()
                      {
                           console.log("function");
                           alert("hi");
                           getThread(post.data.permalink);
                       });
items.push(section[0].outerHTML);
$('#posts').empty().append(items.join(''));
var节=$('').append('');
section.find('a.link').attr('title',post.data.permalink)
.text(post.data.title)
.live(“单击”,函数()
{
console.log(“函数”);
警报(“hi”);
getThread(post.data.permalink);
});
items.push(节[0].outerHTML);
$('#posts').empty().append(items.join('');

注意
.live(“click”,function(){…})的用法那里?这应该能解决你的问题。

看起来没问题。您可以尝试使用each()对该集合进行迭代。我不确定是否可以将处理程序绑定到这样的jQuery集合

section.find('a.link').each(function(){
    $(this).attr('title', post.data.permalink)
                      .text(post.data.title)
                      .click(function()
                      {
                           console.log("function");
                           alert("hi");
                           getThread(post.data.permalink);
                       });


});

看起来不错。您可以尝试使用each()对该集合进行迭代。我不确定是否可以将处理程序绑定到这样的jQuery集合

section.find('a.link').each(function(){
    $(this).attr('title', post.data.permalink)
                      .text(post.data.title)
                      .click(function()
                      {
                           console.log("function");
                           alert("hi");
                           getThread(post.data.permalink);
                       });


});

如果在IE中运行此命令,请确保存在开发人员工具(F12),否则控制台将未定义,调用
console.log()
将强制执行错误并停止函数的执行


还有:outerHTML仅限于IE,您最好忘记它,永远不要使用它。

如果在IE中运行它,请确保存在开发工具(F12),否则控制台将未定义,调用
console.log()
将强制出错并停止函数的执行


还有:outerHTML仅限于IE,您最好忘记它,永远不要使用它。

您不需要手动加入HTML

您将事件绑定到一个元素,然后获取该元素的HTML并将其关联成一个字符串,然后在
附加到#posts时,将该字符串作为新元素插入DOM中

在这里没有定义,但我将从一个肢体出发,假设它是一个生成的
等的数组

如果是这样的话,我可能会错过一些您在这里尝试做的事情,但似乎您可以消除HTML的全部内容,并简单地附加items数组,这将保留绑定的单击事件

// Push the element, don't stringify it.
items.push(section);

// Then simply append the "items" elements.
$('#posts').empty().append(items);

当然,
live
可能也会解决这个问题,但是您当然可以将事件绑定到生成的元素,然后将它们插入DOM中。您不能做的是将事件绑定到元素,然后打印出它的HTML并将其插入DOM中。您与原始元素的任何绑定都将丢失。

您不需要手动加入HTML

您将事件绑定到一个元素,然后获取该元素的HTML并将其关联成一个字符串,然后在
附加到#posts时,将该字符串作为新元素插入DOM中

在这里没有定义,但我将从一个肢体出发,假设它是一个生成的
等的数组

如果是这样的话,我可能会错过一些您在这里尝试做的事情,但似乎您可以消除HTML的全部内容,并简单地附加items数组,这将保留绑定的单击事件

// Push the element, don't stringify it.
items.push(section);

// Then simply append the "items" elements.
$('#posts').empty().append(items);
当然,
live
可能也会解决这个问题,但是您当然可以将事件绑定到生成的元素,然后将它们插入DOM中。您不能做的是将事件绑定到元素,然后打印出它的HTML并将其插入DOM中。您对原始元素所做的任何绑定都将丢失。

这就是为什么我将
if(typeof console==“undefined”){console={log:function(){};}
添加到我的页面:-)这就是为什么我将
if(typeof console==“undefined”){console={log:function(){};}
添加到我的页面:-)