(急,急,急,急!)关于selection 的问题,欢迎各位指教!!!(100分) 难,不过如果你的网页内容不是很复杂,可以试试永远是的方法遍历document.all的每个元素,看那个image在你的范围内。我只知道这种方法,感兴趣,帮你up,顺便棒你写写,看能不能实现,应该没问题, 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 麻烦。。我只知道有这个方法document.selection.createRange().text如果是选中图片的话,这个是不会显示任何内容的。 使用document.selection.createRange().text,如果是选中图片的话,这个是不会显示任何内容的。但是使用document.selection.createRange().htmlText就不一样了,他会将HTML代码显示出来。你只要作一些字符串的处理工作就能从中取出src 试试看这个,我模仿的,楼上说的我试了,不行,还是我不会用,看看这个<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD><TITLE> New Document </TITLE><META NAME="Generator" CONTENT="EditPlus"><META NAME="Author" CONTENT=""><META NAME="Keywords" CONTENT=""><META NAME="Description" CONTENT=""><SCRIPT LANGUAGE="JavaScript"><!--var selecting=false;var x1,x2,y1,y2function mDown(){ selecting=true; x1=window.event.x; y1=window.event.y; selectBG.style.left=x1; selectBG.style.top=y1; for(i=0;i<document.images.length;i++)document.images[i].style.border=0;}function mMove(){ if(!selecting)return false; selectBG.style.visibility="visible"; x2=window.event.x; y2=window.event.y; if(x2<x1||y2<y1){ selectBG.style.left=x2; selectBG.style.top=y2; var w=x1-x2; var h=y1-y2; }else{ var w=x2-x1; var h=y2-y1; } try{ selectBD.style.width=w; selectBD.style.height=h; } catch(x){}}function mUp(){ if(!selecting){ selecting=false; return false }else{ checkSelection(x1,y1,x2,y2); selecting=false; } selectBG.style.visibility="hidden"; selectBD.style.width=1; selectBD.style.height=1; //document.selction.ontimeerror();}var selectedIMG=new Array();function checkSelection(x1,y1,x2,y2){//只选中完整的图片 var imgs=document.images; selectedIMG=new Array(); for(i=0;i<imgs.length;i++){ var imgx1=getAbsoluteX(imgs[i]); var imgy1=getAbsoluteY(imgs[i]); var imgx2=imgx1+imgs[i].clientWidth; var imgy2=imgy1+imgs[i].clientHeight; if((imgx1>=x1&&imgx2<=x2&&imgy1>=y1&&imgy2<=y2) ||(imgx1>=x2&&imgx2<=x1&&imgy1>=y2&&imgy2<=y1)){ selectedIMG[selectedIMG.length]=imgs[i]; imgs[i].style.border=1; imgs[i].style.borderStyle="dashed"; imgs[i].style.borderColor="red"; } } test(selectedIMG)//测试选择,显示选择对象的信息 //如果你想得到img的集合,定义一个全局变量如 :myimgs=selectedIMG}function getAbsoluteX(e){//得绝对X var end=document.body.sourceIndex; var x=e.offsetLeft; var sid=e.parentElement.sourceIndex; var tmp=e.parentElement; while(sid!=end){ x+=tmp.offsetLeft; tmp=tmp.parentElement; sid=tmp.sourceIndex; } return x}function getAbsoluteY(e){//得绝对Y var end=document.body.sourceIndex; var y=e.offsetTop; var sid=e.parentElement.sourceIndex; var tmp=e.parentElement; while(sid!=end){ y+=tmp.offsetTop; tmp=tmp.parentElement; sid=tmp.sourceIndex; } return y}function imgdragstart(){return false;}/*------------------------------------------*/function test(imgarr){ var str='你选择的图片ID是下列:'; for(i=0;i<imgarr.length;i++)str+='<br>id:'.bold()+imgarr[i].id+'|width:'.bold()+imgarr[i].clientWidth+'|height:'.bold()+imgarr[i].clientHeight+'|src:'.bold()+imgarr[i].src; document.all.msg.innerHTML=str }document.onmousedown = mDowndocument.onmousemove = mMovedocument.onmouseup = mUp;//--></SCRIPT></HEAD><BODY><!-- 必须 --><div id=selectBG style="position:absolute;overflow:visible;visibility:hidden;z-index:200;"><TABLE id=selectBD style="border-style:dashed;border-color:red;width:1px;height:1px;border-width:1px;"><TR><TD></TD></TR></TABLE></div><IMG id=img1 SRC="http://www.csdn.net/images/csdn.gif" WIDTH="150" HEIGHT="70" BORDER=0 ALT="" ><IMG id=img2 SRC="http://www.csdn.net/images/ad/vsnet_120.gif" WIDTH="120" HEIGHT="60" BORDER=0 ALT=""><IMG id=img4 SRC="http://www.csdn.net/images/home/programmer200207small.jpg" WIDTH="100" HEIGHT="142" BORDER=0 ALT=""><IMG id=img5 SRC="http://www.csdn.net/images/index_login2.gif" WIDTH="60" HEIGHT="20" BORDER=0 ALT=""><IMG id=img6 SRC="http://www.csdn.net/images/biaoshi.gif" WIDTH="62" HEIGHT="62" BORDER=0 ALT=""><div style="position:absolute;overflow:visible;z-index:1;left:250px;top:300px;width:200px;height:200px;background-color:blue;"> <IMG id=img3 SRC="http://www.csdn.net/images/csdn.gif" WIDTH="150" HEIGHT="70" BORDER=0 ALT=""></div><br><span id=msg style="font-size:13px;"></span><SCRIPT LANGUAGE="JavaScript"><!--for(i in document.images)document.images[i].ondragstart=imgdragstart;//必须,屏蔽image的拖动//--></SCRIPT></BODY></HTML> TO skyover(嘻哈呵嘿):<img>对象都得到了,还有什么不能显示的,只要是这个对象有的就能显示。好,我马上就加一个点选的,有道理。谢谢! 差点瞎忙活,你说的点选不就是click事件的srcElement吗?还有什么可做的,不用做了.不过确实有个缺陷就是,选择的时候会有一个阴影,不知道怎么不让window的选择失去作用? “ llrock(百乐宝) ”做的已经很不错了,鼓励一下!呵呵 IE浏览器是否对onload事件中执行的js方法有限制???? 各位老大啊,帮帮我吧,下载了一下午的1st javascript editor,反倒没有下载成功,电脑中了假软件的木马数据全没有了,赠给我这个软件吧 再次询问YUI的问题,谢谢 javascript工程师 为什么我取iframe的document.body.scrollTop一直都是0? 有点急,js怎么计算两个日期的间隔时间啊? 怎样让这段代码读取数据库里的内容? 页面加载前显示Loading的问题 求助:如何自动选择最快服务器 Jquery this and $(this) check 数组问题 对面的高手看过来......
我只知道有这个方法document.selection.createRange().text如果是选中图片的话,这个是不会显示任何内容的。
但是使用document.selection.createRange().htmlText就不一样了,他会将HTML代码显示出来。你只要作一些字符串的处理工作就能从中取出src
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<SCRIPT LANGUAGE="JavaScript">
<!--
var selecting=false;
var x1,x2,y1,y2
function mDown(){
selecting=true;
x1=window.event.x;
y1=window.event.y;
selectBG.style.left=x1;
selectBG.style.top=y1;
for(i=0;i<document.images.length;i++)document.images[i].style.border=0;
}
function mMove(){
if(!selecting)return false;
selectBG.style.visibility="visible";
x2=window.event.x;
y2=window.event.y;
if(x2<x1||y2<y1){
selectBG.style.left=x2;
selectBG.style.top=y2;
var w=x1-x2;
var h=y1-y2;
}else{
var w=x2-x1;
var h=y2-y1;
}
try{
selectBD.style.width=w;
selectBD.style.height=h;
}
catch(x){}
}
function mUp(){
if(!selecting){
selecting=false;
return false
}else{
checkSelection(x1,y1,x2,y2);
selecting=false;
}
selectBG.style.visibility="hidden";
selectBD.style.width=1;
selectBD.style.height=1;
//document.selction.ontimeerror();
}
var selectedIMG=new Array();
function checkSelection(x1,y1,x2,y2){//只选中完整的图片
var imgs=document.images;
selectedIMG=new Array();
for(i=0;i<imgs.length;i++){
var imgx1=getAbsoluteX(imgs[i]);
var imgy1=getAbsoluteY(imgs[i]);
var imgx2=imgx1+imgs[i].clientWidth;
var imgy2=imgy1+imgs[i].clientHeight;
if((imgx1>=x1&&imgx2<=x2&&imgy1>=y1&&imgy2<=y2)
||(imgx1>=x2&&imgx2<=x1&&imgy1>=y2&&imgy2<=y1)){
selectedIMG[selectedIMG.length]=imgs[i];
imgs[i].style.border=1;
imgs[i].style.borderStyle="dashed";
imgs[i].style.borderColor="red";
}
}
test(selectedIMG)//测试选择,显示选择对象的信息
//如果你想得到img的集合,定义一个全局变量如 :myimgs=selectedIMG
}
function getAbsoluteX(e){//得绝对X
var end=document.body.sourceIndex;
var x=e.offsetLeft;
var sid=e.parentElement.sourceIndex;
var tmp=e.parentElement;
while(sid!=end){
x+=tmp.offsetLeft;
tmp=tmp.parentElement;
sid=tmp.sourceIndex;
}
return x
}
function getAbsoluteY(e){//得绝对Y
var end=document.body.sourceIndex;
var y=e.offsetTop;
var sid=e.parentElement.sourceIndex;
var tmp=e.parentElement;
while(sid!=end){
y+=tmp.offsetTop;
tmp=tmp.parentElement;
sid=tmp.sourceIndex;
}
return y
}
function imgdragstart(){return false;}
/*------------------------------------------*/
function test(imgarr){
var str='你选择的图片ID是下列:';
for(i=0;i<imgarr.length;i++)str+='<br>id:'.bold()+imgarr[i].id+'|width:'.bold()+imgarr[i].clientWidth+'|height:'.bold()+imgarr[i].clientHeight+'|src:'.bold()+imgarr[i].src;
document.all.msg.innerHTML=str
}
document.onmousedown = mDown
document.onmousemove = mMove
document.onmouseup = mUp;
//-->
</SCRIPT>
</HEAD><BODY>
<!-- 必须 -->
<div id=selectBG style="position:absolute;overflow:visible;visibility:hidden;z-index:200;">
<TABLE id=selectBD style="border-style:dashed;border-color:red;width:1px;height:1px;border-width:1px;">
<TR><TD></TD></TR>
</TABLE>
</div>
<IMG id=img1 SRC="http://www.csdn.net/images/csdn.gif" WIDTH="150" HEIGHT="70" BORDER=0 ALT="" >
<IMG id=img2 SRC="http://www.csdn.net/images/ad/vsnet_120.gif" WIDTH="120" HEIGHT="60" BORDER=0 ALT=""><IMG id=img4 SRC="http://www.csdn.net/images/home/programmer200207small.jpg" WIDTH="100" HEIGHT="142" BORDER=0 ALT="">
<IMG id=img5 SRC="http://www.csdn.net/images/index_login2.gif" WIDTH="60" HEIGHT="20" BORDER=0 ALT="">
<IMG id=img6 SRC="http://www.csdn.net/images/biaoshi.gif" WIDTH="62" HEIGHT="62" BORDER=0 ALT="">
<div style="position:absolute;overflow:visible;z-index:1;left:250px;top:300px;width:200px;height:200px;background-color:blue;">
<IMG id=img3 SRC="http://www.csdn.net/images/csdn.gif" WIDTH="150" HEIGHT="70" BORDER=0 ALT="">
</div>
<br>
<span id=msg style="font-size:13px;"></span><SCRIPT LANGUAGE="JavaScript">
<!--
for(i in document.images)document.images[i].ondragstart=imgdragstart;//必须,屏蔽image的拖动//-->
</SCRIPT>
</BODY>
</HTML>
<img>对象都得到了,还有什么不能显示的,只要是这个对象有的就能显示。
好,我马上就加一个点选的,有道理。谢谢!
还有什么可做的,不用做了.不过确实有个缺陷就是,选择的时候会有一个阴影,不知道怎么不让window的选择失去作用?
呵呵