Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jquery防止div内所有链接的默认值_Jquery - Fatal编程技术网

使用jquery防止div内所有链接的默认值

使用jquery防止div内所有链接的默认值,jquery,Jquery,我有一个div看起来像: <div id="blah"> <a href="#" >hello</a> <a href="#"> world</a></div> 我想使用e.preventDefault()禁用此div内的所有链接 如何使用jquery选择div id=“blah”中的所有href标记?以下内容将为您提供定义了href属性的id blah中的所有元素 $('#blah a[href]').click

我有一个div看起来像:

<div id="blah"> <a href="#" >hello</a>   <a href="#"> world</a></div>

我想使用e.preventDefault()禁用此div内的所有链接


如何使用jquery选择div id=“blah”中的所有href标记?

以下内容将为您提供定义了
href
属性的id blah中的所有
元素

$('#blah a[href]').click(function(e) { e.preventDefault(); });
如果没有jQuery,类似于下面的东西(很明显,我在jQuery世界生活的时间太长了,因为我最初在香草JavaScript中使用默认值。不幸的是,不是跨浏览器:()我在Firefox、IE和Chrome中测试了这一点

var anchors = document.getElementById('blah').getElementsByTagName('a');

for(var i=0; i < anchors.length; i++) {
    addEvent(anchors[i], 'click', preventDefault);
}

function preventDefault(e) {
    e = e || window.event;
   (e.preventDefault)? 
       e.preventDefault() : e.returnValue = false;
}

function addEvent(obj, evType, fn){ 
   if (obj.addEventListener){ 
       obj.addEventListener(evType, fn, false); 
       return true; 
   } 
   else if (obj.attachEvent){ 
       var r = obj.attachEvent("on"+evType, fn); 
       return r; 
 } else { 
       return false; 
 } 
}
非jQuery(我没有对此进行测试):

// jQuery.Event is based on DOM3 Events as specified by the ECMAScript Language Binding
// http://www.w3.org/TR/2003/WD-DOM-Level-3-Events-20030331/ecma-script-binding.html
jQuery.Event.prototype = {
    preventDefault: function() {
        this.isDefaultPrevented = returnTrue;

        var e = this.originalEvent;
        if( !e )
            return;
        // if preventDefault exists run it on the original event
        if (e.preventDefault)
            e.preventDefault();
        // otherwise set the returnValue property of the original event to false (IE)
        e.returnValue = false;
    }
$('#blah a').click(function(e){ e.preventDefault(); });
    var addEvent = (function() {
    function addEventIE(el, ev, fn) {
        return el.attachEvent('on' + ev, function(e) {
        return fn.call(el, e);
        });
    }
    function addEventW3C(el, ev, fn) {
        return el.addEventListener(ev, fn, false);
    }
    return window.addEventListener ? addEventW3C:addEventIE;
    })();

var anchors = document.getElementById('blah').getElementsByTagName('a');
for ( var i = anchors.length; i--; ) {
    addEvent( anchors[i], 'click', function(e) { 
        e = e || window.event;
        if ( e.preventDefault ) e.preventDefault()
        else e.returnValue = false
    });
}