Js实现鼠标右键选择的元素属性读取 htmljqueryjs 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <!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>window.onload=function(){ var aObj=document.getElementsByTagName('span'); for(var i=0;i<aObj.length;i++) { aObj[i].oncontextmenu=function() { if(this.getAttribute('title')) { alert(this.getAttribute('title')); } return false; } } document.oncontextmenu=function(){ return false; }}</script></head> <body> <p id="test1" title="1">11111111111111111111</p> <span title="aaaa">aaaaaaaaaaaaaaaaaaaaaaa</span> <span id="test3">sadf23423411</span> <span title="ddd">ddddddddddddddddddddddddddddd</p></body></html> <html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> </head> <script type="text/javascript"> $(function(){ $('#aaaa').mousedown(function(e){ if(3 == e.which){//鼠标右击 if($(this).attr('title')){ alert($(this).attr('title')) } } }) $('#test3').mousedown(function(e){ if(3 == e.which){//鼠标右击 alert($(this).attr('title')) } }) }) </script> <body> <p id="test1" title="1">11111111111111111111</p> <span title='aaaa' id='aaaa'>aaaaaaaaaaaaaaaaaaaaaaa</span> <span id="test3">sadf23423411</span> <span title="ddd">ddddddddddddddddddddddddddddd</sapn> </body></html> document.oncontextmenu=function(e){ var target = e.target if(target.nodeName=='SPAN'){ if(target.getAttribute('title')) { alert(target.getAttribute('title')); } } return false; } 非常感激几位。不过alert提示可否改成在元素附近显示的浮动提示信息 。我试着改了下 var viewer = document.createElement("div"); viewer.innerHtml=this.getAttribute('title'); viewer.style.display = "block";好像有问题。初涉js,谢谢各位。 才发现 <span title="ddd">ddddddddddddddddddddddddddddd</p> 结束标记什么的写错误 。 <!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><style>.alert{position:absolute;width:50px;height:30px;line-height:30px;background:#ccc; text-align:center;}</style><script>window.onload=function(){ var aObj=document.getElementsByTagName('span'); var oCur=null; for(var i=0;i<aObj.length;i++) { aObj[i].oncontextmenu=function(ev) { if(this.getAttribute('title')) { var oDiv=document.createElement('div'); oDiv.className='alert'; oDiv.style.left=this.offsetLeft+this.offsetWidth+'px'; oDiv.style.top=this.offsetTop+'px'; oDiv.innerHTML=this.getAttribute('title'); document.body.appendChild(oDiv); oCur=oDiv; } return false; } } document.oncontextmenu=function(){ return false; }}</script></head> <body> <p id="test1" title="1">11111111111111111111</p> <span title="aaaa">aaaaaaaaaaaaaaaaaaaaaaa</span> <span id="test3">sadf23423411</span> <span title="ddd">ddddddddddddddddddddddddddddd</p></body></html> <!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 src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script><style>.alert{position:absolute;width:50px;height:30px;line-height:30px;background:#ccc; text-align:center;}</style><script>window.onload=function(){ var aObj=document.getElementsByTagName('span'); var oCur=null; for(var i=0;i<aObj.length;i++) { aObj[i].oncontextmenu=function(ev) { if(this.getAttribute('title')) { var oDiv=document.createElement('div'); oDiv.className='alert'; oDiv.style.left=this.offsetLeft+this.offsetWidth+'px'; oDiv.style.top=this.offsetTop+'px'; oDiv.innerHTML=this.getAttribute('title'); document.body.appendChild(oDiv); oCur=oDiv; $(document).click(function() { $('.alert').remove(); }); } return false; } } document.oncontextmenu=function(){ return false; }}</script></head> <body> <p id="test1" title="1">11111111111111111111</p> <span title="aaaa">aaaaaaaaaaaaaaaaaaaaaaa</span> <span id="test3">sadf23423411</span> <span title="ddd">ddddddddddddddddddddddddddddd</span></body></html>修改一下,OK了 谢谢各位。 请教个问题 jquery dialog 不能提交表单 JS抽取N组随机数 并且定于一个值 prompt弹出对话框被IE拦截有什么办法吗? 【【【为什么层内的flash新闻图片位置晃动??】】】 超链接提交表单问题 javascript如何保存网页中的修改 已知图片地址 怎么给图片放大呢 关于ajax跨域问题 关于window.open()的一个小问题。 怎么获取一个tag的绝对坐标? Sencha touch中tabpanel组件停靠问题 二维数据展示问题
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script>
window.onload=function()
{
var aObj=document.getElementsByTagName('span');
for(var i=0;i<aObj.length;i++)
{
aObj[i].oncontextmenu=function()
{
if(this.getAttribute('title'))
{
alert(this.getAttribute('title'));
}
return false;
}
}
document.oncontextmenu=function(){
return false;
}
}
</script>
</head>
<body>
<p id="test1" title="1">11111111111111111111</p>
<span title="aaaa">aaaaaaaaaaaaaaaaaaaaaaa</span>
<span id="test3">sadf23423411</span>
<span title="ddd">ddddddddddddddddddddddddddddd</p>
</body>
</html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head>
<script type="text/javascript">
$(function(){
$('#aaaa').mousedown(function(e){
if(3 == e.which){//鼠标右击
if($(this).attr('title')){
alert($(this).attr('title'))
}
}
})
$('#test3').mousedown(function(e){
if(3 == e.which){//鼠标右击
alert($(this).attr('title'))
}
})
})
</script>
<body>
<p id="test1" title="1">11111111111111111111</p>
<span title='aaaa' id='aaaa'>aaaaaaaaaaaaaaaaaaaaaaa</span>
<span id="test3">sadf23423411</span>
<span title="ddd">ddddddddddddddddddddddddddddd</sapn>
</body>
</html>
var target = e.target
if(target.nodeName=='SPAN'){
if(target.getAttribute('title'))
{
alert(target.getAttribute('title'));
}
}
return false;
}
我试着改了下
var viewer = document.createElement("div");
viewer.innerHtml=this.getAttribute('title');
viewer.style.display = "block";好像有问题。初涉js,谢谢各位。
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
.alert{position:absolute;width:50px;height:30px;line-height:30px;background:#ccc; text-align:center;}
</style>
<script>
window.onload=function()
{
var aObj=document.getElementsByTagName('span');
var oCur=null;
for(var i=0;i<aObj.length;i++)
{
aObj[i].oncontextmenu=function(ev)
{
if(this.getAttribute('title'))
{
var oDiv=document.createElement('div');
oDiv.className='alert';
oDiv.style.left=this.offsetLeft+this.offsetWidth+'px';
oDiv.style.top=this.offsetTop+'px';
oDiv.innerHTML=this.getAttribute('title');
document.body.appendChild(oDiv);
oCur=oDiv;
}
return false;
}
}
document.oncontextmenu=function(){
return false;
}
}
</script>
</head>
<body>
<p id="test1" title="1">11111111111111111111</p>
<span title="aaaa">aaaaaaaaaaaaaaaaaaaaaaa</span>
<span id="test3">sadf23423411</span>
<span title="ddd">ddddddddddddddddddddddddddddd</p>
</body>
</html>
<!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 src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>
<style>
.alert{position:absolute;width:50px;height:30px;line-height:30px;background:#ccc; text-align:center;}
</style>
<script>
window.onload=function()
{
var aObj=document.getElementsByTagName('span');
var oCur=null;
for(var i=0;i<aObj.length;i++)
{
aObj[i].oncontextmenu=function(ev)
{
if(this.getAttribute('title'))
{
var oDiv=document.createElement('div');
oDiv.className='alert';
oDiv.style.left=this.offsetLeft+this.offsetWidth+'px';
oDiv.style.top=this.offsetTop+'px';
oDiv.innerHTML=this.getAttribute('title');
document.body.appendChild(oDiv);
oCur=oDiv;
$(document).click(function() {
$('.alert').remove();
});
}
return false;
}
}
document.oncontextmenu=function(){
return false;
}}
</script>
</head>
<body>
<p id="test1" title="1">11111111111111111111</p>
<span title="aaaa">aaaaaaaaaaaaaaaaaaaaaaa</span>
<span id="test3">sadf23423411</span>
<span title="ddd">ddddddddddddddddddddddddddddd</span>
</body>
</html>
修改一下,OK了 谢谢各位。