Javascript 没有jQuery我能做到这一点吗?
我在我的网站上使用以下代码。我想知道我是否需要jQuery来完成它,或者标准javascript是否可以处理这个过程Javascript 没有jQuery我能做到这一点吗?,javascript,jquery,target,Javascript,Jquery,Target,我在我的网站上使用以下代码。我想知道我是否需要jQuery来完成它,或者标准javascript是否可以处理这个过程 <script type='text/javascript'> //<![CDATA[ $(window).load(function(){ $("a[href^='http']").click(function(event) { event.preventDefault(); // preve
<script type='text/javascript'>
//<![CDATA[
$(window).load(function(){
$("a[href^='http']").click(function(event) {
event.preventDefault(); // prevent the link from opening directly
// open a pop for the link's url
var popup = window.open( this.href , "", "toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no,status=no,width=340,height=10,left=250,top=175" );
// popup.blur();
// window.focus();
}); }); //]]>
</script>
// 你可以试试这个
<div id="divid" onclick="showpop();">click me</div>
<script type="text/javascript">
function showpop(){
window.open(arguments);
return false;
}
</script>
document.getElementById(eleID).onClick = function (){
//do stuff
}
点击我
函数showpop(){
打开(参数);
返回false;
}
document.getElementById(eleID).onClick=function(){
//做事
}
是的,而且它相对简单:只需使用document.getElementsByTagName('a')
并遍历您得到的数组,为任何具有href
属性且值以http
开头的元素设置onclick
。例如,让它成为一个通过
中的onload
属性调用的函数。jQuery只不过是JavaScript代码。当然,标准JS可以处理这个问题,但它需要更多的工作。我会选择jQuery。如果没有它,您将不得不在document.getElementsByTagName('a')
上循环,并检查href中是否有正则表达式。首先,jQuery选择器不是DIV,选择器会获取多个元素!!!我不认为仅仅为了摆脱jQuery而从不引人注目的JS转向内联事件处理程序是一个好主意javascript@amiuhle出于好奇,我们还可以在Javascript中使用脚本标记中的事件处理程序,我假设将函数赋值给一个变量并重新使用它会消耗更少的内存。对的
var hrefs = document.getElementsByTagName('a');
for (i in hrefs) {
if (hrefs[i].href && hrefs[i].href.match(/^http/)) {
hrefs[i].onclick = function(){
var popup = window.open( this.href , "", "toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no,status=no,width=340,height=10,left=250,top=175" );
return false;
}
}
}