开发者

Pass a global javascript array value into a HTML Tag

Just wondering if it's possible to pass a global javascript array value into a html tag, specifically inside a img (title) tag?

Basically want to know if I can do this:

<img src="_info.gif" height="26" width开发者_Python百科="37" title=myArray[5]/>

If so, how and if not, can people possibly provide other suggestions.

Thanks


No, it is not possible. I suggest that you set the title property using javascript. In order to do it you need to:

1) Add JavaScript hook to the element. For example css class or id 2) get the element via the hook and sets its title

Example:

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

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

You might as well use some kind of JavaScript library, for example jQuery or Prototype


I would do it this way:

<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>


Could also do:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜