开发者

Assign javascript function to a dom element

开发者 https://www.devze.com 2022-12-20 17:23 出处:网络
I\'m using the MSHTML library for parsing out HTML via MSHTML.HTMLDocument. my question: is there a way to assign a JavaScript function to a DOM element?

I'm using the MSHTML library for parsing out HTML via MSHTML.HTMLDocument.

my question: is there a way to assign a JavaScript function to a DOM element? I've tried something like:

div.onmouseover = "function(){alert('mouseover')}"

and

div.setattribute "onmouseover开发者_开发技巧" , "function(){alert('mouseover')}"

without success (no error, but no effect either).

Any ideas if it's possible?


Always use a function, rather than attaching a string of code in your javascript. You can do this by assigning an anonymous function. The function receives an event object as it's only argument, which has information about the event, if you need to know the x/y positions of where the mouse was.

div.onmouseover = function(event) {
    //do stuff
};


Have you tried not using anonymous functions? For example instead of

div.onmouseover = "function(){alert('mouseover')}"

Use something like

div.onmouseover = "alert('mouseover');"


try using the

<script>

tag in your string


You are defining the function but not invoking it. You have to invoke the function by including the invocation parentheses. Use

div.setattribute "onmouseover" , "function(){alert('mouseover')}()"


div.onmouseover = function(){alert("mouseover")}


Try to use,

button.onclick = new Function(“someFunctionName(‘input” + counter + “‘)”); 

instead of function(){alert("mouseover") , this will work for dynamic created controls.

Note the spelling difference between function and new Function


Try this:

div.setattribute "onmouseover" , "alert('mouseover');"
0

精彩评论

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

关注公众号