开发者

jquery v javascript

I have a function which I need to appear within a jQuery $(document).ready(function() {} - I am au fait with javascript but not really worked with jQuery. How can I jQuerify this function?

function populateContext()
{
    contextTxtBox = document.getElementById(开发者_StackOverflow'searchContext');
    pathArr = window.location.pathname.split( '/' );
    contextTxtBox.value = pathArr[1].toUpperCase(); 
};


jQuerify? Make it a plugin!

(function($){

    $.fn.populateContext = function(){
        var pathArr = window.location.pathname.split( '/' );
        return this.val(pathArr[1].toUpperCase());
    };


}(jQuery));

and use it like this

$(document).ready(function(){
    // Same as window.onload
    $("#searchContext").populateContext();
});


It's actually nearly identical since the only thing I find worth jQuerifying (nice word) is the DOM element.

function populateContext()
{
    var contextTxtBox = $('#searchContext');
    var pathArr = window.location.pathname.split('/');
    contentTxtBox.val(pathArr[1].toUppercase());
}

$(document).ready(function()
{
    populateContext();
});


this way, if i understand you correctly

function populateContext()
{
    contextTxtBox = $('#searchContext');
    pathArr = window.location.pathname.split( '/' );
    contextTxtBox.val(pathArr[1].toUpperCase()); 
};


$(document).ready(function() {
//whatever code you want

});

function populateContext()  {
    pathArr = window.location.pathname.split( '/' );
    $("#searchContext").Val(pathArr[1].toUpperCase()); 
};

just a sidenote: jQuery IS javascript so you can mix and match :)


This should do the trick

function populateContext() {
    var aPath = $( location ).attr( 'href' ).split( "/" );
    $( '#searchContext' ).val( aPath[1].toUpperCase() )
}


function populateContext(){
    contentTxtBox = $('#searchContext');
    pathArr = window.location.pathname.split( '/' );
    $(contextTxtBox).val(pathArr[1].toUpperCase());
}


I am not sure aout what you mean, but you could just do:

$(document).ready(function() {
    populateContext()
});

If you want to improve your function to make use of jQuery, you could do this way:

function populateContext() {
    var $contextTxtBox = $('#searchContext');
    pathArr = window.location.pathname.split( '/' );
    $contextTxtBox.val(pathArr[1].toUpperCase()); 
};

If you provide more information, about what exactly is your doubt, i may be able to explain better.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜