未捕获的TypeError:无法设置null的属性'onclick',输入type =“img”

时间:2015-05-28 19:03:42

标签: javascript webrtc

请帮我找到错误,正常按钮“重拨”工作正常,但“挂掉”一个没有..

<div id="buttons">
    <button type="button" id="Redial">Call</button>
    <input type="image" src="hangup.png" alt="hangoff">
</div>
var hangoff = document.querySelector("#hangoff");
var Redial = document.querySelector("#Redial");

hangoff.onclick = Dropcall;
Redial.onclick = Callagain;

function Dropcall() {
    phone.hangup();
    remoteStream.parentNode.removeChild(remoteStream);
}

function Callagain() {
    window.location.reload();
}

2 个答案:

答案 0 :(得分:1)

您正在选择一个不存在的ID。

div id="buttons">
    <button type="button" id="Redial">Call</button>
    <input type="image" src="hangup.png" alt="hangoff"> <-- does not have id hangoff.
</div>

var hangoff = document.querySelector("#hangoff");
var Redial = document.querySelector("#Redial");

只需将输入标记更改为:

<input type="image" src="hangup.png" alt="hangoff" id="hangoff">

这将成功。 (注意新的&#34; id&#34;属性)

答案 1 :(得分:1)

hangoff = document.querySelector("#hangoff")

此选择器正在寻找ID 挂起

的HTMLElement

如果你想获得属性 alt = hangoff 的元素,你必须使用下一个seletor

hangoff = document.querySelector("[alt='hangoff']")

或设置输入ID

<input type="image" src="hangup.png" id="hangoff">