如何定义一个函数以稍后在Jquery中使用

时间:2019-09-28 13:43:36

标签: javascript jquery

在普通的Javascript中,定义一个函数然后在以后调用它非常容易,但是我对Jquery还是陌生的,能帮帮我吗?

下面是纯Javascript,但我想用Jquery做到

<script>
function displayMe(){document.getElementById('demo').innerHTML = "Display"};
</script>

<button type="button" onclick="displayMe()">
    Click me to add the word "Display" to the page.
</button>

<p id="demo"></p>

然后在这里运行一个Jquery,但不是我想要做的,

<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"
        integrity="sha256-pasqAKBDmFT4eHoN2ndd6lN370kFiGUFyTiUHWhU7k8="
        crossorigin="anonymous"></script>
  
<script>
$(document).ready(function (){
    $('#displayMe').click(function(){
        $("#demo").html('Display');
    });
});
</script>

<button type="button" id="displayMe">
    Click me to add the word "Display" to the page.
</button>

<p id="demo"></p>

,现在我尝试稍后定义和使用

<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"
        integrity="sha256-pasqAKBDmFT4eHoN2ndd6lN370kFiGUFyTiUHWhU7k8="
        crossorigin="anonymous"></script>
  
<script>
$(document).ready(function (){
    $('#displayMe').click(displayfunction(){ });
});

function displayFunction (){
    $("#demo").html('Display')
};
</script>

<button type="button" id="displayMe">
    Click me to add the word "Display" to the page.
</button>

<p id="demo"></p>

它显示以下错误:

{
  "message": "Uncaught SyntaxError: missing ) after argument list",
  "filename": "https://stacksnippets.net/js",
  "lineno": 20,
  "colno": 41
}

我尝试添加),只是出现其他错误。

2 个答案:

答案 0 :(得分:2)

您需要传递#Add stringsAsFactors = FALSE to data.frame to solve ghost factor issue downstream in plotly df1<- data.frame ( Vehicle_type=c("CAR","CAR","CAR","MOTORCYCLE","MOTORCYCLE","MOTORCYCLE"),Brand_Name=c("ford","Honda","Audi","SYM","Honda","Harley"),revenues=(as.integer(c("526","552","445","222","223","300"))),cost=(as.integer(c("426","427","325","172","178","235"))),profit=(as.integer(c("100","125","120","50","45","65"))),branch1=(as.integer(c("10","15","12","6","5","5"))),branch2=(as.integer(c("3","4","7","6","4","9"))), stringsAsFactors = FALSE) shinyServer(function(session,input, output) { observe({ print(input$ProductId) #df2<-df1%>%filter(Vehicle_type==input$ProductId) %>% select(Brand_Name) df2<-df1%>%filter(Vehicle_type==input$ProductId) #updateSelectInput(session,"nameId","Vehicle Brand Name:",choices=unique(df2)) updateSelectInput(session,"nameId","Vehicle Brand Name:",choices=unique(df2$Brand_Name)) print(input$IndicatorId) print(df2) #IndicatorSum<- reactive({df2 %>% pull(input$IndicatorId) %>% sum()}) IndicatorSum<- df2 %>% pull(input$IndicatorId) %>% sum() print(IndicatorSum) #browser() df3<-mutate(df2,!!quo_name(paste0(input$IndicatorId,'_perctcontcl')):=(!!sym(input$IndicatorId)/IndicatorSum)*input$bins) print(df3) #To check why we get ghost factor in plotly, we add stringsAsFactors = FALSE to data.frame to solve this issue print(str(df3)) output$Test2 <- renderPlotly({ Test2 <- plot_ly( df3, x = ~Brand_Name, y = ~get(paste0(input$IndicatorId,'_perctcontcl')), #Here I think you need the updated Indicator after we multiply by input$bin #y = ~get(input$IndicatorId), type = "bar",color=~Brand_Name)}) }) 作为displayFunction的参数

$('#displayMe').click()
function displayFunction() {
    $("#demo").html('Display')
}

$(document).ready(function () {
    $('#displayMe').click(displayFunction);
});

答案 1 :(得分:1)

您可以仅将函数的名称提供给data_cols处理程序,名称为click,而不是displayFunction

displayfunction
$(document).ready(function() {
  $('#displayMe').click(displayFunction);
});

function displayFunction() {
  $("#demo").html('Display')
};

或者,就像在第一个示例中一样,只需使用<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha256-pasqAKBDmFT4eHoN2ndd6lN370kFiGUFyTiUHWhU7k8=" crossorigin="anonymous"></script> <button type="button" id="displayMe"> Click me to add the word "Display" to the page.</button> <p id="demo"></p>属性:

onclick
function displayFunction() {
  $("#demo").html('Display')
};