动态添加元素的调用函数

问题描述:

我对javascript和jquery很新,所以这可能是一个非常简单的问题。请不要介意。

I am quite new to javascript and jquery so this might be a really simple problem. Please don't mind.

我在分区中追加新的链接(a)元素,其中id =whateverby

I am appending new link(a) elements in division with id = "whatever" by

$("#whatever").append(jQuery('<a>').attr('href', 'url').text('blah'));

并且调用函数

$(function () {
  $('#whatever a').tagcloud();
});

在该特定div内的每个链接元素上。
但是这个函数只在旧元素上调用,而不是在我刚刚动态添加的元素上调用。
我试过这样做:

on every link element inside that particular div. But this function is being called only on old elements and not on the ones that I just dynamically added. I tried doing this:

$(document).on("change", '#whatever', function () {
    $("whatever a").tagcloud();  
});

但它仍无效。

它似乎无法按预期工作,因为您缺少添加 rel 属于要追加的元素。

It seems it doesn't work as expected as you are missing adding rel attribute to the to-be-appended element(s).

// ...
// The plugin reads the rel attributes
var tagWeights = this.map(function(){
   return $(this).attr("rel");
});

试试这个:

$('<a>').attr({'href': 'url', 'rel': 'value'})
        .text('blah')
        .tagcloud()
        .appendTo('#someWhere');

http://jsbin.com/uBAzItuj/3