2014-05-14 65 views
2

我想寫一個腳本來切換<td>元素的顯示屬性。
我:href JavaScript不能從Tampermonkey腳本運行

// ==UserScript== 
// @name  Dema VOC hide 
// @version 0.1 
// @include  http://mywebsite.com/* 
// @require http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js 
// ==/UserScript== 

function toggle(){ 
     $("td[style='color:Red; height:50px']").toggle();   
} 
//Avoid conflicts 
$(document).ready(function() 
{  
    $('.hlavicka_vyhledavani:first-child').append("<a href='javascript:toggle()' id='toggle' class='content-link1'><b>B2B</b></a>"); 
    toggle(); 
}); 

.toggle()本身的工作原理,所以jQuery是裝的,但在B2B的鏈接點擊後,我得到錯誤:

Uncaught TypeError: object is not a function

+0

在toggle()中設置一個'debugger',它會幫助你找出錯誤。 – DontVoteMeDown

回答

2

重寫你的一些代碼。由於您使用jQuery的你能夠擺脫內聯JS -

function toggle(){ 
     $("td[style='color:Red; height:50px']").toggle();   
} 

$(document).ready(function() {  
    $('.hlavicka_vyhledavani:first-child').append("<a href='#' id='toggle' class='content-link1'><b>B2B</b></a>"); 

    $('body').on('click', '#toggle', function() { // account for dynamically added elements with event delegation 
     /* you may want to reconsider the name of the function 
     * since there is a function you're calling with that name 
     */ 
     toggle(); 
    }); 
}); 

此外,如果你不重新使用你的函數你總是可以直接撥打電話的點擊功能的改變這樣的 -

$('body').on('click', '#toggle', function() { 
    $("td[style='color:Red; height:50px']").toggle(); 
}); 
+0

非常感謝! – Adrian