簡體   English   中英

將全局javascript數組值傳遞到HTML標簽

[英]Pass a global javascript array value into a HTML Tag

只是想知道是否有可能將全局javascript數組值傳遞到html標簽中,尤其是在img(標題)標簽中?

基本上想知道我是否可以這樣做:

<img src="_info.gif" height="26" width="37" title=myArray[5]/>

如果是這樣,如何以及是否可以,人們是否可以提出其他建議。

謝謝

不,不可能。 我建議您使用javascript設置title屬性。 為此,您需要:

1)將JavaScript鈎子添加到元素。 例如css class或id 2)通過鈎子獲取元素並設置其標題

例:

document.getElementById("myImage").title = myArray[5];

<img id="myImage" src="_info.gif" height="26" width="37"/>

您也可以使用某種JavaScript庫,例如jQuery或Prototype

我會這樣:

<img id="image-1" src="_info.gif" height="26" width="37" />

<script type="text/javascript">
var image = document.getElementById('image-1');
image.setAttribute('title', myArray[5]);
</script>

也可以做:

<img src="_info.gif" height="26" width="37" onload="this.title = myArray[5];" />

暫無
暫無

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

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