سؤال

I have a input that is per-populated with a value when the user first loads the form. I would like to have this one tooltip be persisitent until the user focuses on this one field. I am using the same tooltip across the page and only need this 1 tooltip to show on load of the page until the user focuses on the input below it. Then it can either go away or behave like the others. Is there a way to do this?

HTML:

 <label for="vehiclePrice" class="form-control-label vpl">Vehicle Price <a href="#" title="Customize your vehicle price." class="jqTooltip">tooltip<img src="assets/img/glyphicons3/comment.png" width="10" height="10" /></a></label>
                 <input type="text" class="form-control" name="vehiclePrice" id="vehiclePrice" placeholder="$25592 Suggested MSRP" onkeypress="return isNumberKey(event)" value="25592 Suggested MSRP" required />

JS:

$(function() {
    $(".jqTooltip").tooltip({
        track: true,
        position: {
            my: "center bottom-20",
            at: "center top",
            using: function( position, feedback ) {
                $( this ).css( position );
                $( "<div>" )
                    .addClass( "arrow" )
                    .addClass( feedback.vertical )
                    .addClass( feedback.horizontal )
                    .appendTo( this );
            }
        }

    });

});

There is also some nifty CSS that goes with it you can see in the fiddle:

fiddle

هل كانت مفيدة؟

المحلول

You can achieve this using open and close methods in jQuery UI Tooltip.

I have modified your code for better readability :

HTML

<section>
    <label id="tt1" class="tooltip" for="vehiclePrice" title="">Vehicle Price</label>
    <input type="text" id="vehiclePrice" placeholder="$25592 Suggested MSRP" onkeypress="return isNumberKey(event)" required />
</section>
<br><br><br><br><br><br>
<label id="tt2" class="tooltip" title="APR">Annual Percentage Rate (APR)</label>

JavaScript

$(function () {
    $(".tooltip").tooltip({
        track: true,
        position: {
            my: "center bottom-20",
            at: "center top",
            using: function (position, feedback) {
                $(this).css(position);
                $("<div>")
                    .addClass("arrow")
                    .addClass(feedback.vertical)
                    .addClass(feedback.horizontal)
                    .appendTo(this);
            }
        }
    });
    $( "#tt1" ).tooltip( "option", "content", "vehicle price" );
    $("#tt1").tooltip("open");
    $("#tt1").tooltip().off("mouseleave mouseover");
    $( "#tt1" ).on( "tooltipclose", function( event, ui ) {
    $("#tt1").tooltip("open");
    } );
    $("#vehiclePrice").focus(function () {
        $("#tt1").tooltip("close");
    });
});

See in JSFiddle.

نصائح أخرى

You can create your own div styled like the way you want and show/hide/fade it when you require instead of using jquery ui tooltip.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top