onclick参数(onclick获取当前对象)
其实onclick参数的问题并不复杂,但是又很多的朋友都不太了解onclick获取当前对象,因此呢,今天小编就来为大家分享onclick参数的一些知识,希望可以帮助到大家,下面我们一起来看看这个问题的分析吧!
详解a标签添加onclick事件的几种方式
我们常用的在a标签中有点击事件:
1. a rel="external nofollow" href="javascript:js_method();" rel="external nofollow"
这种方法在传递this等参数的时候很容易出问题,而且javascript:协议作为a的href属性的时候不仅会导致不必要的触发window.onbeforeunload事件,在IE里面更会使gif动画图片停止播放。W3C标准不推荐在href里面执行 javascript语句
2. a rel="external nofollow" rel="external nofollow" href="javascript:void(0);" rel="external nofollow" rel="external nofollow" onclick="js_method()"
这种方法是很多网站最常用的方法,也是最周全的方法,onclick方法负责执行js函数,而void是一个操作符,void(0)返回undefined,地址不发生跳转。而且这种方法不会像第一种方法一样直接将js方法暴露在浏览器的状态栏。
3.a rel="external nofollow" rel="external nofollow" href="javascript:;" rel="external nofollow" rel="external nofollow" onclick="js_method()"
这种方法跟跟2种类似,区别只是执行了一条空的js代码。
4.a rel="external nofollow" rel="external nofollow" rel="external nofollow" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="js_method()"
这种方法也是网上很常见的代码,#是标签内置的一个方法,代表top的作用。所以用这种方法点击后网页后返回到页面的最顶端。
5.a rel="external nofollow" rel="external nofollow" rel="external nofollow" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="js_method();return false;"
这种方法点击执行了js函数后return false,页面不发生跳转,执行后还是在页面的当前位置。
综合上述,在a中调用js函数最适当的方法推荐使用:
a rel="external nofollow" rel="external nofollow" href="javascript:void(0);" rel="external nofollow" rel="external nofollow" onclick="js_method()"
a rel="external nofollow" rel="external nofollow" href="javascript:;" rel="external nofollow" rel="external nofollow" onclick="js_method()"
a rel="external nofollow" rel="external nofollow" rel="external nofollow" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="js_method();return false;"
以上所述是小编给大家介绍的a标签添加onclick事件的几种方式详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
addEventListener和普通.onclick的区别
addEventListener是在 FireFox上的用法。
addEventListener的参数一共有三个,语法为:
element.addEventListener(type,listener,useCapture)
详解
其中element是要绑定函数的对象。
type是事件名称,要注意的是"onclick"要改为"click","onblur"要改为"blur",也就是说事件名不要带"on"。
listener当然就是绑定的函数了,记住不要跟括号
最后一个参数是个布尔值,表示该事件的响应顺序,下面重点介绍一下addEventListener的第3个参数(useCapture)。
userCapture若为true,则浏览器采用Capture,若为false则采用bubbing方式。建议用false
区别:
varbtn1Obj=document.getElementById("btn1");
//element.addEventListener(type,listener,useCapture);
btn1Obj.addEventListener("click",method1,false);
btn1Obj.addEventListener("click",method2,false);
btn1Obj.addEventListener("click",method3,false);执行顺序为method1->method2->method3
document.getElementById("btn").onclick=method1;
document.getElementById("btn").onclick=method2;
document.getElementById("btn").onclick=method3;如果这样写,那么将会只有medhot3被执行
使用JSON.stringify() 将数据传递给 onclick 函数
使用 JSON.stringify()将数据传递给 onclick函数是一种安全且有效的方法,可避免直接嵌入对象导致的 [object Object]错误,并确保数据完整传递。
以下是具体实现方式及关键注意事项的详细说明:
核心实现步骤动态生成 HTML时嵌入 JSON字符串
在模板字符串中,通过 `${JSON.stringify(song)}`将对象转换为 JSON格式的字符串。
确保 onclick属性使用单引号包裹,JSON字符串使用双引号,避免语法冲突。
list+= `<div class="songitem" style="background:${song.background};"><img src=${song.coverpath} alt="${song.SongName}"><span class="name">${song.SongName}</span><span class="songlistplay"><span class="songTime">${song.time}</span><i class="far fa-play-circle songitemplay" onclick='makeAllPlays(${JSON.stringify(song)})'></i></span></div>`;事件处理函数接收解析后的对象
在 makeAllPlays函数中,参数 song会自动解析为 JavaScript对象,可直接访问其属性(如 song.SongName)。
function makeAllPlays(song){ console.log("歌曲信息:", song);//输出完整对象//示例:播放歌曲逻辑}关键注意事项引号嵌套规则
onclick属性值必须用单引号(')包裹。
JSON字符串内部使用双引号("),例如:onclick='makeAllPlays({"SongName":"我的歌","time":"3:30"})'
数据安全性与转义
若对象属性包含用户输入(如未过滤的文本),需对 JSON字符串进行转义,防止 XSS攻击。
示例转义方法(使用 textContent或库如 he):function escapeHtml(unsafe){ return unsafe.toString().replace(/[&<>'"]/g,(char)=>({'&':'&','<':'<','>':'>',"'":''','"':'"'}[char]));}const safeJson= escapeHtml(JSON.stringify(song));
性能优化
避免传递大型对象:仅传递必要属性(如{ id: 1, name:"歌名"}),减少 HTML体积。
替代方案:若数据量极大,可通过 data-*属性存储唯一标识(如 data-songid),在事件中通过 ID查询完整数据。
<i class="songitemplay" data-songid="${song.id}" onclick="makeAllPlaysById(this)"></i>function makeAllPlaysById(element){ const songId= element.dataset.songid; const song= fetchedSongs.find(s=> s.id== songId);//从全局数组查询 console.log(song);}
完整代码示例async function loadSongs(){ const res= await fetch('./songApi/songs.json'); const fetchedSongs= await res.json(); let list=""; fetchedSongs.forEach((song)=>{//转义 JSON字符串(可选,根据数据安全性需求) const escapedJson= JSON.stringify(song).replace(/</g,'u003c').replace(/>/g,'u003e'); list+= `<div class="songitem" style="background:${song.background};"><img src=${song.coverpath} alt="${song.SongName}"><span class="name">${song.SongName}</span><span class="songlistplay"><span class="songTime">${song.time}</span><i class="far fa-play-circle songitemplay" onclick='makeAllPlays(${escapedJson})'></i></span></div>`;}); document.getElementById('songListContainer').innerHTML= list;}function makeAllPlays(song){ console.log("播放歌曲:", song.SongName);//实际播放逻辑(如调用音频API)}window.onload= loadSongs;总结优势:JSON.stringify()确保对象以标准格式传递,避免解析错误,适合动态内容生成。适用场景:需传递简单对象且数据量较小时(如配置项、元数据)。扩展建议:对于复杂交互,结合 data-*属性或事件委托(Event Delegation)可提升性能与可维护性。
关于onclick参数,onclick获取当前对象的介绍到此结束,希望对大家有所帮助。