开发者

Can I embed javascript as an xhtml attribute?

I want to update this code 开发者_运维知识库to call javascript in order to validate the url of my image. This is PHP echoing xhtml with embedded javascript.

  echo "<img class = \"c\" src=\"$fav\" alt=\"\"\/><a name = \"a1\" class = \"b\" href = \"$ass_array[url]\">$ass_array[name]</a>";

This is what I have. Will it work?

  echo "<img class = \"c\" src=\"<script type=\"text/javascript\">validate_fav($fav)</script>\" alt=\"\"\/><a name = \"a1\" class = \"b\" href = \"$ass_array[url]\">$ass_array[name]</a>";

Thanks.


No, that is invalid XHTML. You end up setting the src to <script type= and then you have invalid markup.

You should do something like:

echo "<img id=\"theimg\" class = \"c\" src=\"$fav\" alt=\"\"\/><a name = \"a1\" class = \"b\" href = \"$ass_array[url]\">$ass_array[name]</a>";

?>

<script type="text/javascript">
    document.getElementById('theimg').setAttribute('src', validate_fave($fav));
</script>

<?php
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜