<html> <head> </head> <body> <a id="TestLink" href="http://www.csdn.net">csdn</a> <script> var testLink = document.getElementById("TestLink"); testLink.attachEvent("onmouseover",mouseEnter); function mouseEnter(){ var e = event; var src = e.srcElement; var id = src.id; openWindow(src.href); }function openWindow(url){ window.open(url,"",""); } </script> </body> </html>
稍微来了点兴趣,出于研究目的,试了试。 为了代码简单,使用了框架。 仅供参考。不推荐这样干。 右键的问题,你怕是没办法直接解决了,因为你无法用JS获取用户究竟点了哪个右键菜单。 除非你自己实现一个JS构建的右键菜单,那你又会碰到非常麻烦的兼容性问题。 下面的代码实现了一点点简单的拖拽逻辑,部分事件代码看起来多余,去掉试试就会发现会碰到很多兼容性问题。 还是那句话,干这种吃力不讨好的事情真的没啥必要。 回去冷静2天你就会觉得这个想法真的没啥好的。 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/mootools/1.2.4/mootools-yui-compressed.js"></script> </head> <body> <div><a href="http://www.baidu.com" target="_blank">www.baidu.com</a></div> <div><a href="http://www.google.cn" target="_blank">www.google.cn</a></div> </body> </html> <script type="text/javascript"> /*<![CDATA[*/ //获取页面上选中文字的相关信息 function getSelected() { var obj = {}; if (window.getSelection) { // This technique is the most likely to be standardized. // getSelection() returns a Selection object, which we do not document. obj.text = window.getSelection().toString(); obj.element = window.getSelection().focusNode.parentNode; }else if (document.getSelection) { // This is an older, simpler technique that returns a string obj.text = document.getSelection(); obj.element = null; }else if (document.selection) { // This is the IE-specific technique. // We do not document the IE selection property or TextRange objects. obj.text = document.selection.createRange().text; obj.element = document.selection.createRange().parentElement(); } return obj; } window.addEvent('domready',function(){ var enableDrag = false; var enableDragOpen = false; var url = null;
<div id="div1">
<a href="http://hi.csdn.net/sohightthesky" ondrag="return doDrag(this)">手动?</a>
</div>
</body>
<script type="text/javascript">
var doDrag=function(obj) {
window.open(obj.href);
return false;
}
</script>
<head>
</head>
<body>
<a id="TestLink" href="http://www.csdn.net">csdn</a>
<script>
var testLink = document.getElementById("TestLink");
testLink.attachEvent("onmouseover",mouseEnter);
function mouseEnter(){
var e = event;
var src = e.srcElement;
var id = src.id;
openWindow(src.href);
}function openWindow(url){
window.open(url,"","");
}
</script>
</body>
</html>
<a href="http://hi.csdn.net/space-home.html" target="_blank">csdn</a>
为了代码简单,使用了框架。
仅供参考。不推荐这样干。
右键的问题,你怕是没办法直接解决了,因为你无法用JS获取用户究竟点了哪个右键菜单。
除非你自己实现一个JS构建的右键菜单,那你又会碰到非常麻烦的兼容性问题。
下面的代码实现了一点点简单的拖拽逻辑,部分事件代码看起来多余,去掉试试就会发现会碰到很多兼容性问题。
还是那句话,干这种吃力不讨好的事情真的没啥必要。
回去冷静2天你就会觉得这个想法真的没啥好的。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/mootools/1.2.4/mootools-yui-compressed.js"></script>
</head>
<body>
<div><a href="http://www.baidu.com" target="_blank">www.baidu.com</a></div>
<div><a href="http://www.google.cn" target="_blank">www.google.cn</a></div>
</body>
</html>
<script type="text/javascript">
/*<![CDATA[*/ //获取页面上选中文字的相关信息
function getSelected() {
var obj = {};
if (window.getSelection) {
// This technique is the most likely to be standardized.
// getSelection() returns a Selection object, which we do not document.
obj.text = window.getSelection().toString();
obj.element = window.getSelection().focusNode.parentNode;
}else if (document.getSelection) {
// This is an older, simpler technique that returns a string
obj.text = document.getSelection();
obj.element = null;
}else if (document.selection) {
// This is the IE-specific technique.
// We do not document the IE selection property or TextRange objects.
obj.text = document.selection.createRange().text;
obj.element = document.selection.createRange().parentElement();
}
return obj;
} window.addEvent('domready',function(){
var enableDrag = false;
var enableDragOpen = false;
var url = null;
$$('a').addEvents({
'mousedown':function(event){
enableDrag = true;
event.stop();
},
'mousemove':function(event){
if(enableDrag){
enableDragOpen = true;
url = this.href;
}
enableDrag = false;
event.stop();
},
'mouseup':function(event){
url = null;
enableDrag = false;
enableDragOpen = false;
event.stop();
}
});
document.addEvents({
'mousemove':function(event){
if(enableDragOpen){
event.stop();
}
},
'mouseup':function(){
if(enableDragOpen){
window.open(url);
}
url = null;
enableDrag = false;
enableDragOpen = false;
},
'keydown':function(event){
if(event.control && event.key == 'v'){
var selected = getSelected();
if(selected.element){
if($(selected.element).get('tag')=='a'){
window.open(selected.element.href);
}
}
}
}
});
});/*]]>*/
</script>
Google工具栏 也有这个功能是感应英文单词的
纯js实现很难 除非用activeX
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head><body onmouseup="showSeletedLink()">
http://www.baidu.com
<script language="javascript" type="text/javascript">function showSeletedLink()
{
var linkText=document.selection.createRange().text;
window.open(linkText,'','','');
}
</script>
</body>
</html>
您的代码就快满足条件,呵呵真行,不过可能是因为我没说清楚,我的设想是当浏览者选择了“http://www.abc.com ”,它打开的是“http://www.abc.com/123.html ”,因为“http://www.abc.com”这些字符是包含在“http://www.abc.com/123.html ”链接中而您的代码是选择什么字符,就打开什么,再者,您能不能把点击右键就打开网页的功能删除,感激不尽!
你没上网?或者没有加载该库?