我有一個jQuery代碼,當點擊任何元素時,點擊任何元素都可以在點擊和返回時更改元素類型。點擊切換元素類型
問題是CodePen中的某些原因在點擊時根本沒有改變,但是在原始網站中只做了一次就沒有任何結果。
的HTML
<div>
<span class="input-group-btn">
<button type="submit" class="btn btn-primary"><span class="btn"aria-hidden="true">Go</span></button>
</span>
</div>
的Javascript:
jQuery(document).ready(function() {
$.fn.changeElementType = function(newType) {
var attrs = {};
$.each(this[0].attributes, function(idx, attr) {
attrs[attr.nodeName] = attr.nodeValue;
});
this.replaceWith(function() {
return $("<" + newType + "/>", attrs).append($(this).contents());
});
}
$('.input-group-btn button').changeElementType('div');
$(function() {
$(".input-group div").on("click", function(e) {
$('.input-group-btn div').changeElementType('button');
e.stopPropagation()
});
$(document).on("click", function(e) {
if ($(e.target).is(".input-group input") === false) {
$('.input-group-btn button').changeElementType('div');
}
});
});
});
這裏是筆: http://codepen.io/florinsimion/pen/AXxKbG
任何想法?
在控制檯上:'pen.js:4 Uncaught TypeError:無法讀取未定義的屬性'屬性' – vaso123