JS目标不起作用,即8

时间:2012-11-26 03:23:41

标签: javascript internet-explorer-8 target dwr

我有这段代码,其中数据是包含来自响应servlet的行的DWR对象。 img.onclick使用IE9工作,但我需要它也适用于IE8。有些想法? 谢谢!

function functionA(data){
  var getPrintCred = function(data) { 
                        var img = document.createElement("img");
                        img.src = "images/image1.jpg";
                        img.style.width="20px";
                        img.style.height="18px";
                        img.alt = data.field1;
                        img.title = getRegimen;
                        img.onclick = function(target) { functionB(target) };
                        return img;
  };
}

function functionB(data){
                var var1= data.target.title;
                var var2= data.target.alt;
                if ( var1 != null && var1 == "IM")
                    var1 = "valueA";
                else
                    var1 = "valueB";
                functionC(var2,var1);
}

function functionC(param1, param2){
 alert ('Using IE 9 works, but IE8 no works...help me!'+param1+'-'param2);
}

2 个答案:

答案 0 :(得分:1)

即8不支持事件对象中的target属性,您必须使用srcElement属性。

function functionB(data){
            var var1= (data.target || data.srcElement).title;
            var var2= data.target.alt;
            if ( var1 != null && var1 == "IM")
                var1 = "valueA";
            else
                var1 = "valueB";
            functionC(var2,var1);
}

此外,看起来没有事件对象传递给onclick事件处理程序,因此您可以将window.event作为后备传递。

img.onclick = function(target) { functionB(target || event) };

http://jsfiddle.net/mowglisanu/wkB6K/

答案 1 :(得分:0)

尝试以下

function functionA(data){
    var getPrintCred = function(data) { 
        var img = document.createElement("img");
        img.src = "images/image1.jpg";
        img.style.width="20px";
        img.style.height="18px";
        img.alt = data.field1;
        img.title = getRegimen;
        img.onclick = function(target) {
            target = target || window.event;
            functionB(target);
        };
        return img;
    };
}

function functionB(data){
    var var1= data.target.title;
    var var2= data.target.alt;
    if ( var1 != null && var1 == "IM")
        var1 = "valueA";
    else
        var1 = "valueB";
    functionC(var2,var1);
}

function functionC(param1, param2){
    alert ('Using IE 9 works, but IE8 no works...help me!'+param1+'-'param2);
}

在IE中,事件对象不作为参数传递给事件处理程序方法,但它在全局属性window.event对象中可用。