我的页面上有一个字段,其id为orit_c_discountrate我需要在此字段后添加一个图像,并使用onlcick调用另一个名为UpdateQuotedPrice的Javascript函数,我在下面写了这个:
function AddPercentImage(){
var img = document.createElement('IMG');
img.setAttribute('src', 'Themes/Img/default/icons/recent_order.gif');
img.onclick = function(){
UpdateQuotedPrice();
}
//This is the line that errors
document.getElementById('orit_c_discountrate').appendChild(img)
}
并使用此attachEvent
window.attachEvent("onload",AddPercentImage);
但是我在控制台中出现以下错误
SCRIPT65535:意外调用方法或属性访问。
元素:
<input name="orit_c_discountrate" class="EDIT" id="orit_c_discountrate" type="text" size="4" maxLength="4"/>
谁能告诉我我做错了什么?
由于
答案 0 :(得分:3)
您不能将图像附加为input
的子元素...您可以将输入包裹在<span>
(或其他元素,例如div
)中并附加想象一下......例如:
HTML:
<span><input name="orit_c_discountrate" class="EDIT" id="orit_c_discountrate" type="text" size="4" maxLength="4"/></span>
JavaScript的:
function AddPercentImage() {
var img = document.createElement('IMG');
img.setAttribute('src', 'Themes/Img/default/icons/recent_order.gif');
img.onclick = function() {
UpdateQuotedPrice();
};
//This is the line that errors
document.getElementById('orit_c_discountrate').parentNode.appendChild(img);
}