分配锚标记以动态调用Javascript函数 [英] Assigning Anchor tag's to call Javascript function dynamically

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

问题描述

我正在动态创建<span class="infa9span"><img src="/csm/view/include/images/foldericon.png"/><a id="infa9Service">'+servicename+'</a><br/></span>标签的列表并将其附加到div

I am creating list of <span class="infa9span"><img src="/csm/view/include/images/foldericon.png"/><a id="infa9Service">'+servicename+'</a><br/></span> tags dynamically and appending it to a div

然后使用下面的地图将a标签属性映射到某些功能

Then using the below map to map a tags attribute to some function

var idMap = {
            //it can be a lot more
            "javaInfo":javaInfo,

            /*infa9 product map*/
            "infa9PMServer":infa9PMServer,
            "infa9Service":infa9Service
        };

这是点击处理程序

$('#ds-accordion a').click(function(event) {
    var elementId=$(this).attr("id");
    treeItemClickHandler(elementId);
});

function treeItemClickHandler(id)
{
    (idMap[id])(id);    //Is this usage called 1st class functions?
}

function infa9Service(id)
{  
    alert("i got this "+id);
}

注意:我正在使用Jquery v1.6.3

Note: I am using Jquery v1.6.3

但是,当我单击任何a标记时,它将调用该函数并执行该函数内的所有操作,但在treeItemClickHandler函数中给出错误Object dosen't support this porperty or method.

But when I click on any of the a tags, it calls the function an does all the operation inside the function, but gives an error Object dosen't support this porperty or method in the treeItemClickHandler function.

我想知道,

  1. 如何避免出现此错误?
  2. 对于这样的事情,是否有更好的方法?
  3. 我正在使用的是一等函数吗(如果可以的话,我在哪里可以了解到更多信息)?

谢谢.

更新

如何传递第二个参数?

'<span class="infa9span"><img src="/csm/view/include/images/foldericon.png"/><a id="infa9Service" title='+servicename+'>'+servicename+'</a><br/></span>'

$('#ds-accordion a').click(function(event) {
    var elementId=$(this).attr("id");
    var elementName=$(this).attr("title");
    treeItemClickHandler(elementId,elementName);
});

function treeItemClickHandler(id,name)
{
    idMap[id](id,name);
}

function infa9Service(id,name)
{
  alert(id+", "+name);
}

它给了我infa9Service, undefined

推荐答案

签出 http://jsfiddle.net/ywQMV/4

1)定义您的功能.

2)定义您的ID映射.

2)define your id map.

html部分:

<div id ="ds-accordion">    
    <span class="infa9span">
         <img src="/csm/view/include/images/foldericon.png"/>
         <a id="infa9Service" title='+servicename+'>'+servicename+'</a>
         <br/>
    </span>

js部分:

function infa9Service(id, serviceName)
{  
    alert("i got this "+id +" serviceName : " + serviceName);
}


var idMap = {
            "infa9Service":infa9Service
        };

$('#ds-accordion a').click(function(event) {
    var elementId=$(this).attr("id");
    var serviceName = this.title;
    treeItemClickHandler(elementId, serviceName);
});

function treeItemClickHandler(id,serviceName)
{
   // alert(idMap[id])
    (idMap[id])(id,serviceName);    //Is this usage called 1st class functions?
}

这篇关于分配锚标记以动态调用Javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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