How to call a jQuery function from Dart?

2020-02-11 01:54发布

问题:

This is a typical situation in jQuery:

$(".myClass").myFunction({
    aKey: 'some value'
});

How do you call that using dart:js?

The documentation is a bit cryptic and a similar question that I found here seems dated.

回答1:

You can do :

main() {
  js.context.callMethod(r'$', ['.myClass'])
      .callMethod('myFunction', [new js.JsObject.jsify({'aKey': 'some value'})]);
}


回答2:

You can use the build-in funcitons querySelector or querySelectorAll instead of the jQuery selector. So it would be:

main(){   
    querySelector(".myClass").myFunction(){
        aKey: 'some value'
    } 
}

or for mulitple elements:

main(){
    querySelectorAll(".myClass").myFunction(){
        aKey: 'some value'
    } 
}


回答3:

How about use DQuery instead.

DQuery is a porting of jQuery in Dart.