我在jQuery论坛中发现了一个响应,并且他们提供了一个函数来执行此操作,但结果并不相同。 这里是我的图像按钮创建了一个例子:如何使用jQuery获得outerHTML以使其跨浏览器
var buttonField = $('<input type="image" />');
buttonField.attr('id', 'butonFshi' + lastsel);
buttonField.val('Fshi');
buttonField.attr('src', 'images/square-icon.png');
if (disabled)
buttonField.attr("disabled", "disabled");
buttonField.val('Fshi');
if (onblur !== undefined)
buttonField.focusout(function() { onblur(); });
buttonField.mouseover(function() { ndryshoImazhin(1, lastsel.toString()); });
buttonField.mouseout(function() { ndryshoImazhin(0, lastsel.toString()); });
buttonField.click(function() { fshiClicked(lastsel.toString()); });
我有这样的情况:
buttonField[0].outerHTML = `<INPUT id=butonFshi1 value=Fshi src="images/square-icon.png" type=image jQuery15205073038169030395="44">`
代替外功能我发现给出buttonField.outer()= <INPUT id=butonFshi1 value=Fshi src="images/square-icon.png" type=image>
功能是:
$.fn.outer = function(val){
if(val){
$(val).insertBefore(this);
$(this).remove();
}
else{ return $("<div>").append($(this).clone()).html(); }
}
这样喜欢这我松开了我插入的处理程序。 有没有办法使用jquery获得outerHTML,以便让它跨浏览器而不会丢失处理程序?!从https://github.com/darlesson/jquery-outerhtml
var outer = buttonField.parent().html();
这不是创建自定义属性的好东西,比如'jQuery15205073038169030395 = “44”'。相反,使用jQuery函数'.data(element,key,value)'来存储自定义数据。然后,您可以使用'.clone(true)',其中布尔值指示是否要复制数据和事件 – Andre 2011-04-20 15:56:13