简体   繁体   English

在使用jQuery定义的元素上运行JavaScript函数

[英]Run JavaScript function on element defined with jQuery

Not sure I titled this well.. show's that I'm in unfamiliar territory. 不确定我的标题是否很好。表明我在陌生领域。 How can I run a JavaScript function based off of the element called in a jQuery function? 如何基于jQuery函数中调用的元素运行JavaScript函数?

Theory: 理论:

<script type="text/javascript">
  $.fillit('video');
</script>

(run fillit on video tag present in page.. interchangable with other elements) (对页面中显示的视频标签运行fillit。可与其他元素互换)

$.fillit = function(){
  this is where it says "run on the tag defined in the jQuery function"
}):
$.fn.extend({
    fillit : function(){...}
});

then... 然后...

$('.video').fillit();

Edit (after comments) 编辑 (评论后)

To fill a dom element with other elements/html: 用其他元素/ html填充dom元素:

var img = document.createElement('img');
img.setAttribute('src', 'somesrc.jpg');

$('.video').append(img);

or 要么

$('.video').html('<img src="somesrc.jpg"/>');

You can do it the way you described like so 您可以按照您描述的方式进行操作

<script type="text/javascript" language="javascript">
$.fillit = function(content)
{
$("result").html(content);

}

//call function
$.fillit("HELLO WORLD");

</script>

or as Alexander just posted if you want to do it on the selected element. 或亚历山大(Alexander)刚发布的内容(如果要在所选元素上执行此操作)。

I don't think adding functions directly to jquery with $.func = is a good idea though. 我不认为直接使用$ .func =将函数添加到jquery是个好主意。 If jQuery ever adds a fillit method your method will conflict with theirs. 如果jQuery添加了fillit方法,则您的方法将与其冲突。

technopeasant, it sounds like you are using a jquery plugin (in your example, a plugin called 'fillit') and it is asking you to run the plugin on a tag or series of tags. 技术农民,听起来您正在使用jquery插件(在您的示例中为名为“ fillit”的插件),并且要求您在一个标签或一系列标签上运行该插件。 Sorry if I misunderstood your question. 对不起,如果我误解了你的问题。

If that is the case, all you need to do is one of two things. 如果真是这样,那么您要做的就是两件事之一。 If you are trying to run it on a very specific element in the HTML page (one with an id like <div id="myvideo"></div>) then all you need to do is run: 如果您试图在HTML页面中一个非常特定的元素上运行它(一个ID如<div id =“ myvideo”> </ div>),则只需执行以下操作:

$('#myvideo').fillit();
//Notice the '#' symbol, that looks up the element with an id of 'myvideo'

If you want to run the plugin on a series of elements (like all <p> tags in the entire document, you'd run something like: 如果要在一系列元素上运行插件(例如整个文档中的所有<p>标记,则应运行以下命令:

$('p').fillit()
//notice no '#', it's just looking up all <p> tags regardless of ID.

Take a look at the jQuery documentation regarding selectors to get a more concrete idea of how these selectors work: 查看有关选择器的jQuery文档,以更具体地了解这些选择器的工作方式:

http://docs.jquery.com/How_jQuery_Works http://docs.jquery.com/How_jQuery_Works

Someone answered with a link to this: http://docs.jquery.com/Plugins/Authoring 有人回答了这个问题: http : //docs.jquery.com/Plugins/Authoring

exactly what I was looking for. 正是我想要的。 claim your kudos! 声称您的荣誉!

(function( $ ){

  $.fn.fillit = function() {

    this.fadeIn('normal', function(){

      var container = $("<div />").attr("id", "filled")
        .appendTo(container);

    });

  };
})( jQuery );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM