开发者

How can i dynamically Change onmouseover, onmouseout and onClick WITH parameters?

开发者 https://www.devze.com 2022-12-16 00:47 出处:网络
I have an image: <img id=\"reqPic\" src=\"mark.png\" /> I also have declared a flag earlier on in the page:

I have an image:

<img id="reqPic" src="mark.png" />

I also have declared a flag earlier on in the page:

<script type="text/javascript">
 var isToolTipEnabled = true;
</script>

I now want to be able to check the flag and if its true, assign the onmouseover and onmouseout events. However, the onmousover event has to be changed to another fun开发者_Python百科ction called Tip('string') which takes in a string. I have seen other questions on here on how to change this but it I dont see how I can pass paramters to the new function I want to change to.

The onClick would be changed to something like this:

onClick="javascript:toggleHelp('reftypeHelp');";

Any help on this would be greatly appreciated.

Thanks!


document.getElementById("reqPic").onmouseover = function() { Tip('This is the string.'); };


You could have an onload event on your body tag which calls a function that checks necessary values.

<script type="text/javascript">
var isToolTipEnabled = true;
function eventAdder() {
    if (isToolTipEnabled) {
        var img = document.getElementById('reqPic');
        img.onmouseover = function() {
            Tip('string');
        }
        img.onmouseout = whateverElse;
    }
}
</script>
<body onload="eventAdder();">


with jQuery you could use the hover function but yeah creating an inline function works too

0

精彩评论

暂无评论...
验证码 换一张
取 消