Getting the active or focused element Id in a web page
Lets say there are two iframes on my webpage and one toolbar for operations like make the text bold, underline, italic 开发者_StackOverflow社区etc. The iframe is set to DesignMode so that user can input text and use it as a text editor. Im trying to use a single toolbar for the iframes....so is there a way that i can knw which iframe did the user edit so that the operation(bold, italic etc) can be applied to that iframe Id?
the algorithm that comes to my mind is that get the active element id first and then apply the operation, but i dont knw the syntax for doing it!!
Thanks!!
ok heres the answer to it...
just register an onClick event with that element passing the id or name and u have the element id in the called function...
<html>
<head>
<script type="text/javascript">
function def()
{
myFrame1.document.designMode="on";
myFrame2.document.designMode="on";
document.getElementById("myFrame1").contentWindow.document.addEventListener('click', function(event) {clic("myFrame1");}, false);
document.getElementById("myFrame2").contentWindow.document.addEventListener('click', function(event) {clic("myFrame2");}, false);
}
function clic(id)
{
alert(id + " is clicked/focused");
}
</script>
</head>
<body onLoad="def()">
<iframe id="myFrame1" border="0">
</iframe>
<iframe id="myFrame2" border="0">
</iframe>
</body></html>
im pretty sure this will work for any other html element too!! (dint check it tho)
精彩评论