English 中文(简体)
Display the right style of a div with JQuery
原标题:

I m trying to display the "right" value of a div named "slider", displaying it inside of "span.display_value"

    var rightStyleValue = $( div.slider ).css( right );
    alert(rightStyleValue); 
    $( .display_value ).html(rightStyleValue); 

UPDATE: Html is posted below. I m using a drag and drop script to drag a div called "slider" that is inside of a div named "container". I want to have it "do something" when the "right" property of the div "slider" reaches 45px(ideally, when the user drags "slider" all the way to the right side of it s parent "container")

Right now, when the page loads it will display the value that i specifically set in the css: "Right:-10". so it will display it once, but i m not sure how to keep updating the displayed value for the "slider" div.

i don t know where to put: $( .display_value ).html(rightStyleValue); to have it continually updated.


<head>
    <style>
        div.container {
        height:15px;
        width:200px;
        overflow:hidden;
        background-color:#858585;
        }

        div.slider {
        height:15px;
        width:20px;
        background-color:#000;
        position:relative;
        right:-10px;
        }
    </style>

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/easing.js"></script>      
<script type="text/javascript" src="js/dragcopy.js"></script>  



<script>

    $().ready(function() {
        $( .slider ).jqDrag();

        $( .slider )
        .jqDrag();
    });

    $(document).ready(function() {

        var rightStyleValue = $( div.slider ).css( right );
        alert(rightStyleValue); 
    $( .display_value ).html(rightStyleValue);  

    $(".slider").mousedown(function() {
        if ($( .slider ).css( right ) == -8 + "px") {
                $( .container ).fadeOut( slow );
        } 

    });
    });




    </script>

</head>
<body>




    <div class="container">
        <div class="slider">

        </div>
    </div>
    <p>slider right:<span class="display_value"> </span></p>




</body>

最佳回答

I think @Ivan Nevostruev is right. Just to suggest one other logical step. Your problem code is doing two things, getting the right style value and setting it to another element.

You should break these down to two separate steps to find which is causing your error ie:

var rightStyleValue = $( div.slider ).css( right );
alert(rightStyleValue);

$( span.display_value ).val(rightStyleValue);

Then, once you know which action is causing the problem you can break it down further, by testing is your selector working, ie:

alert($( div.slider ));

If the result is null or undefined, then you have a problem with your selector.

@Ivan is probably right, but without seeing your html, it could be a bunch of problems. Breaking it down always helps me.

问题回答
$( span.display_value ).text($( div.slider ).css( right ));

use .text(x), not .html() or .val()

Looks like you re having extra bracers after css method call. Also I think val should be replaced with text. Here is example:

$( span.display_value ).text($( div.slider ).css( right ));




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签