找回密码
 注册
搜索
热搜: java php web
查看: 1102|回复: 8

CSS中插入javascript 的方法

[复制链接]
发表于 2009-1-28 22:33:19 | 显示全部楼层 |阅读模式
一些朋友可能知道,CSS中的background-image属性可以插入Javascript.遗憾的是目前只有IE5/6支持.
例如:

复制内容到剪贴板
代码:
<style type="text/css">
#test {
border: 1px solid #000000;
pad**: 10px;
background-image: url('javascript:alert("看到我了么?");')
}</style>
<div id="test">显示消息哦</div>
如果我们在CSS中多个不同的id选择器中都插入了Javascript.那么只有前面的XHTML中调用了该id选择器.才会运行该Javascript代码.而没有调用的id选择器,其中的代码不会运行.

如下,我们定义了两个id选择器.test1和test2.但是并没有在xhtml中调用test2.所以你仍然只能看到test1中的javascript代码.
复制内容到剪贴板
代码:
<style type="text/css">
#test1 {
border: 1px solid #000000;
pad**: 10px;
background-image: url('javascript:alert("看到我了么?");')
}
#test2 {
border: 1px solid #000000;
pad**: 10px;
background-image: url('javascript:alert("你看不到我!");')
}</style>
<div id="test1">显示消息哦</div>
除了显示消息之外,作者试图用这个技巧来生成随机的背景图片.
background-image: url(javascript: document.write('pix/test'+(parseInt(Math.random()*5)+1)+'.gif'));
但是很遗憾,经过几次尝试以失败而告终,证明这种方法是行不通的,不知各位有什么好的方法
发表于 2009-1-28 23:47:53 | 显示全部楼层
水平初级,没太看懂.
回复

使用道具 举报

发表于 2009-1-29 00:03:01 | 显示全部楼层
水平初级,没太看懂.
回复

使用道具 举报

发表于 2009-1-28 23:29:43 | 显示全部楼层
水平初级,没太看懂.
回复

使用道具 举报

发表于 2009-1-28 23:23:11 | 显示全部楼层
学习了,还是没有用到
回复

使用道具 举报

发表于 2009-1-28 23:43:12 | 显示全部楼层
o o o o o o
回复

使用道具 举报

 楼主| 发表于 2009-1-28 23:47:28 | 显示全部楼层
Thanks for sharing this to us.

I just started learning javascript and css for web development, I was thinking, since you can invoke javascript function call from the css, why not give it  a function like
function changebackground(testid ) {
document.getElementById(testid).style.background=url(' pix/test'+(parseInt(Math.random()*5)+1)+'.gif'))';
}

Then call it from the css on the testid which you wanna change the background image?

that's just my thought, I haven't tested it yet, so it might be totally wrong, if you know why it won;t work, could you please kindly drop me a message?

I really wanted to learn from you guys ...thank you
回复

使用道具 举报

发表于 2009-1-28 23:45:56 | 显示全部楼层
表现里面为什么要加上行为呢
回复

使用道具 举报

发表于 2009-1-29 00:02:51 | 显示全部楼层
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|软晨网(RuanChen.com)

GMT+8, 2024-9-20 15:33

Powered by Discuz! X3.5

Copyright © 2001-2023 Tencent Cloud.

快速回复 返回顶部 返回列表