簡體   English   中英

為什么在Firefox,Chrome或Safari上卻不能在IE上運行呢?

[英]Why is this not working on Firefox, Chrome or Safari but on IE?

我是一名學生,無法解決為什么無法正常工作的問題。

function addedToCart(x)
            {
            alert( x.value + " has been added to cart\nPress OK to continue.");
            }


<a href="#" value="blue circle" onclick="addedToCart(this)">

任何幫助表示贊賞!

因為value不是錨點<a>有效屬性。 它旨在用於表單元素,例如inputselect

一個更合適的(並且帶有有效的標記)解決方案是使用HTML5 data-*屬性,然后按照@Jeffrey Sweeney的建議使用getAttribute獲取它:

<a href="#" data-value="blue circle" onclick="addedToCart(this)">

接着:

alert( x.getAttribute("data-value"))

嘗試使用getAttribute函數:

alert( x.getAttribute("value") + " has been added to cart\nPress OK to continue.");

https://developer.mozilla.org/en-US/docs/DOM/element.getAttribute

2 value不是<a </a>結尾

我希望下面的代碼可以幫助您理解一種使其工作的模擬方法:

<script>
    function addedToCart(x) {
        alert(x.value + " has been added to cart\nPress OK to continue.");
    }
</script>

<a href="#" onclick="addedToCart(document.getElementById('txt1'))">blue circle</a>
<input type="text" id="txt1" value="some value"/>

IE也有它自己的方式來詮釋HTML,它增加了多 不好 ,因為它強制不具有圖案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM