3

How to jquery load () in the external javascript function?

 3 years ago
source link: https://www.codesd.com/item/how-to-jquery-load-in-the-external-javascript-function.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to jquery load () in the external javascript function?

advertisements

i'm trying to create my own tool in javascript, which will help me load some php functions/files into my div.

Here is my javascript:

$(document).ready(function () {
    $('.btn').click(function() {
        $('#someid').testit();
        //   $('#someid').load('somefile');  it works
        return false;
    })
});

function testit() {
    $(this).load('somefile');
}

And my html:

<div id="someid"></div>
<form>
    <input type="submit" value="test" class="btn" />
</form>

I can't load the resource from external function - why is that?


If you want to call something like:

$('#someid').testit();

Then, you have to create a jQuery plugin that defines the method testit. That code would look something like this:

jQuery.fn.testit = function() {
    return this.load('somefile');
}

Then, and only then can you use testit() as a jQuery method like this:

$('#someid').testit();

And, it will operate on the desired selector.

Here's some of the jQuery doc on writing plugin methods like this.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK