动态添加元素的调用函数 [英] Call function on dynamically added elements

查看:69
本文介绍了动态添加元素的调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对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

这篇关于动态添加元素的调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆