开发者

Setting element value within an event callback using jQuery

A little stumped by this one so I'm hoping you guys can help.

Looks like I may have found an IE-related jQuery bug. Created a small test (included here) which I can dupe the issue. Works fine in Firefox and Chro开发者_如何转开发me and fails in IE8-9.

Issue is that the DIV "podcastStatus" isn't being updated at the correct moment when the update occurs within an event callback. You can test it locally by using IE tools and Firebug and setting breakpoints where I've commented in the code.

Steps:

  1. Set breakpoints in Firebug and IE
  2. Dev tools according to my comments in the code
  3. Click on either "Next" or "Prev"
  4. Text in the DIV should change to either "Next" or "Prev" depending on which button you clicked
  5. Text in the DIV should then revert back to "Playing..."

I think it's innerHTML-related but I've tried the innerHTML DIV wrapper trick and no-dice.

NOTE: I've used both .text() and .html() with no resolution.

Here's the test case:

<body>

<style type="text/css">
    #podcastStatus {
        background-color: ButtonHighlight;
        font-size: 120pt;
        font-weight: bolder;
        color: Black;
        margin: 0 auto;
        text-align: center;
        position:absolute; top:0; bottom:0; left:0; right:0;
        margin:auto; height:200px; width:100%;
        z-index: 100;
    }
</style>

<div id="podcastStatus">Playing...</div>        

<div id="targetBtns">
<input type="button" name="prev" id="prev" value="Prev"/>
<input type="button" name="next" id="next" value="Next"/>
</div>        

<script>
function onButtonClicked(e) {
    switch (e.target.defaultValue) {
        case "Next":
            // Use IE tools or Firebug...
            //Set breakpoint here. Immediate after this executes, DIV text should be updated to "Prev"....
            $('#podcastStatus').html("Prev").show();
            break;
        case "Prev":
            // Use IE tools or Firebug...            
            //Set breakpoint here. Immediate after this executes, DIV text should be updated to "Next"....            
            $('#podcastStatus').html("Next").show();
            break;
    }

    //Set breakpoint here. DIV should show either "Prev" or "Next" at this point...        
    playPause();
}        

function playPause() {
    $('#podcastStatus').text("Playing...").fadeIn('slow');
}   

$( "#targetBtns" ).click( function(event) { onButtonClicked(event) } );

</script>            

</body>


Your example code causes a div to have its text change to 'Prev' or 'Next' and to be shown. It then calls a function which causes the text of the same DIV to be set to 'Playing...' and tells it to fade into showing--even though it already is showing.

  1. The text changes happen so fast you never see the 'Prev' or 'Next' text
  2. it will not fade in if it's already showing.

With those two issues, the code appears to do nothing in all browsers.

All that said, this is how I would modify your JS for consistency, anyway:

var $podcastStatus = $( '#podcastStatus' );

function playPause()
{
    $podcastStatus.text( 'Playing...' ).fadeIn( 'slow' );
}   

$( '#targetBtns' )
    .delegate( 'input[type="button"]', 'click', function onButtonClicked( e )
    {
        var text;
        switch( $( this ).val() )
        {
            case 'Next':
                text = 'Prev';
                break;

            case 'Prev':
                text = 'Next';
                break;
        }

        $podcastStatus.html( text ).show();

        playPause();

        e.preventDefault();
    } );


Internet Explorer doesn't support the target property. Instead, Microsoft invented srcElement.

If you check for the existence of target and fallback to srcElement, your code should work:

switch ((e.target || e.srcElement).defaultValue) {

But I'd consider using a bit more jQuery and less JavaScript ;)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜