[英]Run JavaScript function on element defined with jQuery
不確定我的標題是否很好。表明我在陌生領域。 如何基於jQuery函數中調用的元素運行JavaScript函數?
理論:
<script type="text/javascript">
$.fillit('video');
</script>
(對頁面中顯示的視頻標簽運行fillit。可與其他元素互換)
$.fillit = function(){
this is where it says "run on the tag defined in the jQuery function"
}):
$.fn.extend({
fillit : function(){...}
});
然后...
$('.video').fillit();
編輯 (評論后)
用其他元素/ html填充dom元素:
var img = document.createElement('img');
img.setAttribute('src', 'somesrc.jpg');
$('.video').append(img);
要么
$('.video').html('<img src="somesrc.jpg"/>');
您可以按照您描述的方式進行操作
<script type="text/javascript" language="javascript">
$.fillit = function(content)
{
$("result").html(content);
}
//call function
$.fillit("HELLO WORLD");
</script>
或亞歷山大(Alexander)剛發布的內容(如果要在所選元素上執行此操作)。
我不認為直接使用$ .func =將函數添加到jquery是個好主意。 如果jQuery添加了fillit方法,則您的方法將與其沖突。
技術農民,聽起來您正在使用jquery插件(在您的示例中為名為“ fillit”的插件),並且要求您在一個標簽或一系列標簽上運行該插件。 對不起,如果我誤解了你的問題。
如果真是這樣,那么您要做的就是兩件事之一。 如果您試圖在HTML頁面中一個非常特定的元素上運行它(一個ID如<div id =“ myvideo”> </ div>),則只需執行以下操作:
$('#myvideo').fillit();
//Notice the '#' symbol, that looks up the element with an id of 'myvideo'
如果要在一系列元素上運行插件(例如整個文檔中的所有<p>標記,則應運行以下命令:
$('p').fillit()
//notice no '#', it's just looking up all <p> tags regardless of ID.
查看有關選擇器的jQuery文檔,以更具體地了解這些選擇器的工作方式:
有人回答了這個問題: http : //docs.jquery.com/Plugins/Authoring
正是我想要的。 聲稱您的榮譽!
(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.