简体   繁体   中英

stopPropagation onclick not working in nested list

i have the following function to swap images on the click of a ul in a nested list, but it doesnt stop bubbling up the list..

function bimageswap (step) {
    step.stopPropagation;
    realstep = parseInt(step) + 1;
    nextsteps = realstep + 1;
    for (iss = nextsteps;iss <= 5; iss++) {
        document.getElementById("step" + iss).className = 'step' + iss;
        alert(iss);
    }
    document.getElementById("step" + realstep).className = 'step' + realstep + 'a';
/*$("#step2").css( 'background-image', 'images/adtl_prodimg/discs/step1_.png');*/
    return false;
}

it is called like this:

<ul onclick='return bimageswap("4")'>

i tried the return because it is what i found in another answer but it still doesnt work. i would greatly appreciate any help thanks!

The stopPropagation method is in the event object, you can't call it on a string. You are also missing the parentheses, so it would just get the stopPropagation property from the string (which returns undefined ) and discard it.

Send the event object from the event handler to the function:

<ul onclick="bimageswap(event, '4');">

Use the event object in the function:

function bimageswap(event, step) {
  event.stopPropagation();
  ...

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