简体   繁体   中英

How to make image under div unclickable using jQuery or JavaScript?

This is my code

<div id="companyLogo">
    <a href="index.php?module=Home&amp;action=index" border="0">
        <img src="themes/default/images/company_logo.png?v=hQ67b-0XWvvSENtpYeZcIw&amp;logo_md5=1d2df0902a895af3e05dbc7c4e6758eb" alt="Company logo" border="0" height="15" width="141">
    </a>
</div>

Now, I want to disable this logo. I am using this jquery code

var o = document.getElementById('companyLogo');
o.onmouseover = function() {
    var a = this.getElementsByTagName('div');
    for (x in a) {
        a[x].onclick = function() { return false; };
    }
};

but, it's not working. Now this image in header div. So, if I replace companyLogo id name of image with the header div, then it works, but it makes whole header div section unclickable. I want only few div's unclickable. So, may I know how to achieve that?

User this:

$('#companyLogo a').click(function(e) {
    e.preventDefault();
});

尝试这个:

$("#companyLogo a").attr("href", "#");

your code works except you have to change div to a .

check this sample

Updated code

var o = document.getElementById('companyLogo');
o.onmouseover = function() {
    var a = this.getElementsByTagName('a');
    for (x in a) {
        a[x].onclick = function() { return false; };
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM