【发布时间】:2016-01-31 14:49:55
【问题描述】:
我已经定义了一个这样的js“类”:
var TelephoneFormGroup = function(targetId, removePhoneCallback) {
this.phone = '';
this.phone_type_options = [];
this.counter = 0;
this.targetId = targetId;
this.form_group = {};
this.removePhone = removePhoneCallback;
this.buildFormGroup = function() {
var outerDiv = document.createElement('div');
outerDiv.className = 'form-group';
var label = document.createElement('label');
label.className = 'label-control col-sm-3';
var innerDiv = document.createElement('div');
innerDiv.className = 'col-sm-8';
var inputId = document.createElement('input');
inputId.type = 'hidden';
inputId.name = 'data[Telephone][' + this.counter + '][id]';
var inputPhoneTypeId = document.createElement('select');
inputPhoneTypeId.name = 'data[Telephone][' + this.counter + '][phone_type_id]';
inputPhoneTypeId.className = 'form-control col-sm-3';
var removePhoneIcon = document.createElement('i');
removePhoneIcon.className = "glyphicon glyphicon-remove";
var removePhoneLink = document.createElement('a');
removePhoneLink.appendChild(removePhoneIcon);
removePhoneLink.title = "Remover Telefone";
removePhoneLink.href = "#";
removePhoneLink.dataset.toggle = "tooltip";
removePhoneLink.onclick = this.removePhone;
label.appendChild(removePhoneLink);
label.innerHTML += 'Telefone ' + this.counter;
var that = this;
Object.keys(this.phone_type_options).forEach( function(key) {
inputPhoneTypeId.options[inputPhoneTypeId.options.length] = new Option(that.phone_type_options[key], key);
});
var inputPhone = document.createElement('input');
inputPhone.type = 'text';
inputPhone.name = 'data[Telephone][' + this.counter + '][phone]';
inputPhone.className = 'form-control phone col-sm-9';
outerDiv.appendChild(label);
outerDiv.appendChild(innerDiv);
innerDiv.appendChild(inputId);
innerDiv.appendChild(inputPhoneTypeId);
innerDiv.appendChild(inputPhone);
this.form_group = outerDiv;
},
this.render = function() {
if (this.targetId == null || this.targetId == '') throw 'Empty target id';
if (typeof(this.targetId) === 'string') {
document.getElementById(this.targetId).appendChild(this.form_group);
} else if (typeof(this.targetId === 'object')) {
this.targetId.appendChild(this.form_group);
};
}
};
我的问题是 removePhoneLink.onclick = this.removePhone; 没有在 webkit 浏览器上触发。在 Firefox (mac) 上正常工作,但在 safari/chrome (mac)、safari/chrome (ipad) 上不工作。
我错过了什么?
谢谢!
【问题讨论】:
标签: javascript google-chrome safari webkit