sli*_*khi 5 javascript onclick
这是我的情况.我在整个网站上都有一些链接.其中一些看起来像这样:
<a target="_blank" onclick="someFunction()" href="/somFile.pdf">some file</a>
有些看起来像这样:
<a target="_blank" href="/somFile.pdf">some file</a>
所有链接都应该在单击时调用someFunction().在onclick属性中具有调用的是旧内容页面.较新的页面附加了一个jQuery click事件,如下所示:
$(document).ready(function(){
$('a[href$=".pdf"]').click(function() {
someFunction();
});
});
Run Code Online (Sandbox Code Playgroud)
所以这就是事情.我可以更新someFunction(),但我无法触及实际链接或jQuery.我需要知道点击链接的href值.我尝试在someFunction()中使用以下内容:
var ev = window.event;
origEl = ev.target || ev.srcElement;
console.log(origEl.href);
Run Code Online (Sandbox Code Playgroud)
但这不起作用.我也尝试过console.log(window.event)
什么也没得到,说这是未定义的.知道我在这里缺少什么,或者在调用函数时没有传递引用它基本上是不可能的?
编辑:要清楚,我不能作为短期甚至中期的解决方案编辑someFunction()
onclick或jQuery代码中的调用黑色,所以我不能将它们更改为someFunction(this)
或类似.我不确定是否有可能从someFunction()中获取href,除非我这样做:(
除回调this.href
内部之外,您不需要任何其他内容click
.
$(document).ready(function()
{
function someFunction(foo)
{
console.log(foo);
}
$('a[href$=".pdf"]').click(function()
{
someFunction(this.href);
});
});
Run Code Online (Sandbox Code Playgroud)
或者,你可以this
指向<a>
内部someFunction
,就像这样:
$(document).ready(function()
{
function someFunction()
{
console.log(this.href);
console.log(event);
}
$('a[href$=".pdf"]').click(someFunction);
});
Run Code Online (Sandbox Code Playgroud)
或者,如果这不适合你,我们可以得到更好的Function.apply
:
$(document).ready(function()
{
function someFunction(event)
{
console.log(this.href);
console.log(event);
}
$('a[href$=".pdf"]').click(function (event)
{
someFunction.apply(this, event);
});
});
Run Code Online (Sandbox Code Playgroud)
这是PURE JavaScript
//assuming links inside of a shared class "link"
for(var i in document.getElementsByClassName('link')) {
var link = document.getElementsByClassName('link')[i];
link.onclick = function(e) {
e.preventDefault();
//using returned e attributes
window.location = e.srcElement.attributes.href.textContent;
}
}
//tested on Chrome v31 & IE 11
//not working in Firefox v29
//to make it work in all browsers use something like:
if (e.srcElement === undefined){
//scrElement is undefined in Firefox and textContent is depreciated
window.location = e.originalTarget.attributes.href.value;
} else {
window.location = e.srcElement.attributes.href.textContent;
}
//Furthermore; when you setAttribute("key" "value");
//you can access it with the above node link.
//So say you want to set an objectId on something
//manually and then grab it later; you can use
//Chrome & IE
//srcElement.attributes.objectid.textContent;
//Firefox
//e.originalTarget.attributes.objectid.value;
//*Note: for some unknown reason the attribute being defined as "objectId" is changed to "objectid" when it's set.
Run Code Online (Sandbox Code Playgroud)
所以我让这个小火慢煮了一段时间,我确实想出了一个短期解决方案……这很老套,我觉得这样做有点肮脏,但有时你只需要做你必须做的事情,直到你能做你需要做的事情做...无论如何,我在这里为其他可能陷入类似困境的人发布我丑陋的解决方案...
请记住,我在 OP 中唯一可以接触的代码是 someFunction() 函数本身。另外,我可以在它所在的同一位置添加一些额外的代码,这就是我所做的......
基本上,解决方案是创建一个额外的单击侦听器,将 href 值放入暴露给该函数的变量中,然后将 someFunction() 代码包装在一个小的超时中,以确保新的单击函数可以完成其任务。
<!-- STUFF I CAN'T TOUCH! (located on-page) -->
<a target="_blank" onclick="someFunction()" href="/somFile.pdf">some file</a>
<a target="_blank" href="/somFile.pdf">some file</a>
<script type='text/javascript'>
$(document).ready(function(){
$('a[href$=".pdf"]').click(function() {
someFunction();
});
});
</script>
<!-- END STUFF I CAN'T TOUCH! -->
/*** STUFF I CAN TOUCH! (located in a script include) ***/
// hackjob!
$(document).ready(function(){
$('a').click(function() {
if ($(this).attr('href')) someFunction.href = $(this).attr('href');
});
});
function someFunction(a) {
// more hackjob!
window.setTimeout("console.log(someFunction.href);",200);
}
/*** END STUFF I CAN TOUCH!***/
Run Code Online (Sandbox Code Playgroud)
所以无论如何,是的,这很丑陋,但希望它可以拯救绝望的人。
归档时间: |
|
查看次数: |
37045 次 |
最近记录: |