1 javascript alerts getelementbyid
function showPrice(){
var a = document.getElementById("product_container15");
if (a == "$1,599.00"){
alert(a);
}
else {
alert("$1,499.00");
}
}
Run Code Online (Sandbox Code Playgroud)
这将一直返回$ 1,499.00.我知道我做错了,或者也许有一种完全不同的方式来写这个.如果id为"product_container15",我希望警报显示$ 1,599.00.如果不是那么警报将显示$ 1,499.00.有人能告诉我这是怎么做到的吗?谢谢!
a 是一个元素 - 你想要的内容?
如果这是HTML标记,请尝试textContent或(对于IE innerText):
alert(a.textContent);
Run Code Online (Sandbox Code Playgroud)
或等效(对于文本节点)nodeValue:
alert(a.nodeValue);
Run Code Online (Sandbox Code Playgroud)
如果a是一个表单元素(input,textarea等...),使用value性能:
alert(a.value);
Run Code Online (Sandbox Code Playgroud)
在你的情况下,一个是html对象,所以它永远不会是"$ 1,599.00"也许你应该尝试这样
function showPrice(){
var a = document.getElementById("product_container15").value;
if (a == "$1,599.00"){
alert(a);
}
else {
alert("$1,499.00");
}
}
Run Code Online (Sandbox Code Playgroud)