开发者

javaScript lose focus when I append <embed>

I need to add beep in my开发者_JAVA百科 script. I add every time, when sound must be.

$("#beep").html("<embed src='button-16.wav' hidden='true' autostart='true' loop='false' id='bp' />")

But when I add , my lost focus.

I tried to focus it again in the following ways.

1)

$("#beep").html("<embed src='button-16.wav' hidden='true' autostart='true' loop='false' id='bp' />");
$("#txt").focus();

2)

$("#beep").html("<embed src='button-16.wav' hidden='true' autostart='true' loop='false' id='bp' />");
$("#bp").ready(function(){
    $("#txt").focus(); 
});

But neither first nor second way works.

HTML code.

<div id="beep"></div>
<input type="text" id="txt" />

So, how can I save focus of textbox after beep?


can you try setting an interval to see if the end of the wav file is grabbing the focus?

setInterval(function() {
  $("#txt").focus(); 
}, 100);

I am only recommending this for troubleshooting purposes ... not as an actual fix. It might be taking longer to execute the .wav than it does to run the next process which is your focus()


I found quite different way to solve this problem. I've wrote *.swf, which play a beep, when JavaScript call its function.

ActionScript code.

package  {
import flash.display.*;
import flash.events.*;
import flash.system.*;
import flash.media.Sound;
import flash.net.URLRequest;
import flash.external.ExternalInterface;

public class Main extends Sprite {
    var s:Sound;
    public function Main() 
    {
        ExternalInterface.addCallback("PlaySound", PlaySound);
        var request:URLRequest = new URLRequest("beep.mp3");
        s = new Sound(request);
    }
    public function PlaySound():void
    {
        s.play();
    }
}
}

JavaScript code.

$("#txt").keyup(function(event){
    if(event.which==13){
     var obj=thisMovie("Main");
     obj.PlaySound();
    }
});

function thisMovie(movieName) {
var movie;
try
{
    movie = document[movieName];
    movie = (movie == null) ? window[movieName] : movie;        
}
catch (e)
{
    return null;
}
return movie;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜