单击按钮时,将div元素添加到网站的其他部分

时间:2016-05-28 14:05:53

标签: javascript jquery html

我正在尝试编写一个jQuery脚本,它为按钮添加了一些功能。 基本上,当我单击一个按钮(例如:设置)时,我想将该特定文本(例如:设置)添加到另一个div(div是使用css规则预定义的),就像谷歌浏览器标签的工作方式一样。

$(document).ready(function() {
    var wButtons = document.getElementById('#wrapper');
    $(".buttonSettings").click(function() {
        var domElement = $('<span>Settings</span>').appendTo(wButtons);
        $(this).after(domElement);
    });
});

代码部分工作,但它不会附加到#wrapper div下的.buttonSettings。另外我应该提一下我有预定义数量的标签(最多6个)。

谢谢!

2 个答案:

答案 0 :(得分:0)

从getElementById删除#,在这种情况下,这是==按钮,

演示:https://jsfiddle.net/

require(dplyr)          # data wrangling
require(stringi)        # string/text processing
require(stringr)        # extracting words
require(shiny)
df <- structure(list(term = c("one of the", "a lot of", "thanks for the", 
                              "to be a", "going to be", "i want to", "out of the", "the end of", 
                              "it was a", "as well as", "some of the", "be able to", "part of the", 
                              "i have a", "i have to", "the rest of", "looking forward to", 
                              "is going to", "thank you for", "this is a"), freq = c(3418, 
                                                                                     2969, 2322, 1851, 1749, 1518, 1514, 1466, 1431, 1376, 1372, 1313, 
                                                                                     1230, 1213, 1093, 1084, 1078, 1054, 1030, 1013), share = c(0.000925656110901077, 
                                                                                                                                                0.000804058804349121, 0.000628839522970246, 0.000501284219215299, 
                                                                                                                                                0.000473660777637795, 0.000411101807006388, 0.000410018534787662, 
                                                                                                                                                0.000397019268162954, 0.000387540636249105, 0.000372645643241627, 
                                                                                                                                                0.000371562371022901, 0.000355584105796698, 0.000333106207258141, 
                                                                                                                                                0.000328502300328556, 0.000296004133766787, 0.000293566771274654, 
                                                                                                                                                0.000291941862946565, 0.000285442229634211, 0.000278942596321858, 
                                                                                                                                                0.000274338689392273), x1 = c("one", "a", "thanks", "to", "going", 
                                                                                                                                                                              "i", "out", "the", "it", "as", "some", "be", "part", "i", "i", 
                                                                                                                                                                              "the", "looking", "is", "thank", "this"), x2 = c("of", "lot", 
                                                                                                                                                                                                                               "for", "be", "to", "want", "of", "end", "was", "well", "of", 
                                                                                                                                                                                                                               "able", "of", "have", "have", "rest", "forward", "going", "you", 
                                                                                                                                                                                                                               "is"), x3 = c("the", "of", "the", "a", "be", "to", "the", "of", 
                                                                                                                                                                                                                                             "a", "as", "the", "to", "the", "a", "to", "of", "to", "to", "for", 
                                                                                                                                                                                                                                             "a")), .Names = c("term", "freq", "share", "x1", "x2", "x3"), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                         20L), class = "data.frame")




server <- function(input, output) {
        tokens <- reactive({
                token <- tolower(input$sentence)
                token <- gsub("[^[:alnum:]['-]", " ", token)
                token <- gsub("^\\s+|\\s+$", "", token)
        })

        output$lastOne <- renderPrint({
                word(tokens(), -1)
        })

        output$lastTwo <- renderPrint({
                word(tokens(), -2)
        })

        output$table <- renderDataTable({
                df1 <- df %>%
                        filter(x1 == lastTwo() & x2 == lastOn())
                return(df1)

        })
}

ui <- navbarPage("Filter",
                 tabPanel("The App",
                          column(8, offset = 4,
                                 textInput(inputId = "sentence", label = "Enter your phrase"),
                                 submitButton("Filter")
                          ),

                          fluidRow(
                                  dataTableOutput('table')
                          )
                 ),

                 tabPanel("How to use")
)


shinyApp(ui = ui, server = server)

答案 1 :(得分:0)

如果要将html字符串附加到元素中,请使用这样的jquery appendTo()方法。

&#13;
&#13;
$("button").click(function() {
     $("<span>Span content</span>").appendTo("#wrapper");
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Append</button>
<br/><br/>
<div id="wrapper">Wrapper content</div>
&#13;
&#13;
&#13;