避免使用JavaScript或jQuery重复结果(avoid repeating of result using JavaScript or jQuery)
大家好,我一直在想如何避免使用for循环在JavaScript或jQuery上显示相同的结果
这是我的代码:
function displayData(e) { var html = ''; var html2 = ''; var x = ''; var mapDiv = document.getElementById('mapContainer'), i = 0, dataIndex, tooltipDiv, key mapMarkers = $(mapDiv).find('.e-mapMarker'), index = 0; var divCont = $(mapDiv).find('#rightdiv'), index = 0; for (i = 0; i < mapMarkers.length; i++) { if (e.target.parentNode.parentNode == mapMarkers[i]) { index = i; } } html += '<div id="infocontainer">'; html += '<div class="p-image"><img src="src/images/retrofit.png"/></div>'; html += '<div class="popupdetail">'; html += '<div class="p-name"> Site Name: ' + flsSites[index].site_name + '</div>'; html += '<div class="p-name"> Site Status: ' + flsSites[index].status + '</div>'; html += '<div class="p-name"> Country: ' + flsSites[index].country_name + '</div>'; html += '</div>'; html += '</div>'; html2 += '<div class="rightcontainer">'; html2 += '<img id="productimage" src="src/images/retrofit.png" onclick="DisplayProfileCard();"/>'; html2 += '<div id="imagedetail">'; html2 + '<input type="hidden" value='+ flsSites[index].serial_number +'/>'; html2 += '<span class="details">Product Type:' + flsSites[index].serial_number +'</span>'; html2 += '<span class="details">Version / Size <img class="row_one_icon lightbulb_icon" id="lightbulb" src="src/images/lightbulb1.png" onClick="LightBulb()" /><img id="convert" class="row_one_icon arrow_icon" src="src/images/arrow_Off.png" onClick="Conversion()"/><img id="lightning" class="row_one_icon" src="src/images/lightningOff.png" onClick="Lightning()"/><img id="bullseye" class="row_one_icon bullseye" src="src/images/bullseye_off.png" onClick="BullsEye()"/></span>'; html2 += '<span class="details">Estimated annual Spend <img class="row_one_icon ribbon" src="src/images/ribbon1.png"/><img class="row_one_icon map" src="src/images/map1.png"/><img class="row_one_icon paper_stack" id="paper" src="src/images/paper_stack_Off.png" onclick="PaperStack()"/><img class="row_one_icon chain" id="chain" src="src/images/chain_Off.png" onClick="ChainLink()"/></span>'; html2 += '<span class="details">Site name / manufacturer</span>'; html2 += '<span class="details">Selling Sales Eng</span>'; html2 += '</div>' html2 += '</div>'; if (!document.getElementById('map_tooltip')) { tooltipdiv = $("<div></div>").attr('id', "map_tooltip"); $(document.body).append(tooltipdiv); $(tooltipdiv).css({ "display": "none", "padding": "5px", "position": "absolute", "z-index": "13000", "cursor": "default", "font-family": "Segoe UI", "color": "#707070", "font-size": "12px", "pointer-events": "none", "background-color": "#FFFFFF", "border": "1px solid #707070" }); } else { tooltipdiv = $("#map_tooltip"); $(tooltipdiv).css({ "left": (e.pageX + 5), "top": (e.pageY + 5) }); $(tooltipdiv).html(html).show("slow"); //$('#defaulttext').hide(); //$('#searchcontainer').append(html2); //$('.rightcontainer').eq($(this).index()).addClass('background'); $(html2).appendTo("#searchcontainer"); }
现在在这段代码中,我能够通过将鼠标悬停在地图上的标记上来显示我的结果,但是如果我返回我之前悬停的标记而不是突出显示其相应的结果,它将在下面进行另一个相同结果的渲染我不需要
Hi guys I have been wondering how can I avoid displaying of the same result on JavaScript or jQuery using for loop
Here is my code:
function displayData(e) { var html = ''; var html2 = ''; var x = ''; var mapDiv = document.getElementById('mapContainer'), i = 0, dataIndex, tooltipDiv, key mapMarkers = $(mapDiv).find('.e-mapMarker'), index = 0; var divCont = $(mapDiv).find('#rightdiv'), index = 0; for (i = 0; i < mapMarkers.length; i++) { if (e.target.parentNode.parentNode == mapMarkers[i]) { index = i; } } html += '<div id="infocontainer">'; html += '<div class="p-image"><img src="src/images/retrofit.png"/></div>'; html += '<div class="popupdetail">'; html += '<div class="p-name"> Site Name: ' + flsSites[index].site_name + '</div>'; html += '<div class="p-name"> Site Status: ' + flsSites[index].status + '</div>'; html += '<div class="p-name"> Country: ' + flsSites[index].country_name + '</div>'; html += '</div>'; html += '</div>'; html2 += '<div class="rightcontainer">'; html2 += '<img id="productimage" src="src/images/retrofit.png" onclick="DisplayProfileCard();"/>'; html2 += '<div id="imagedetail">'; html2 + '<input type="hidden" value='+ flsSites[index].serial_number +'/>'; html2 += '<span class="details">Product Type:' + flsSites[index].serial_number +'</span>'; html2 += '<span class="details">Version / Size <img class="row_one_icon lightbulb_icon" id="lightbulb" src="src/images/lightbulb1.png" onClick="LightBulb()" /><img id="convert" class="row_one_icon arrow_icon" src="src/images/arrow_Off.png" onClick="Conversion()"/><img id="lightning" class="row_one_icon" src="src/images/lightningOff.png" onClick="Lightning()"/><img id="bullseye" class="row_one_icon bullseye" src="src/images/bullseye_off.png" onClick="BullsEye()"/></span>'; html2 += '<span class="details">Estimated annual Spend <img class="row_one_icon ribbon" src="src/images/ribbon1.png"/><img class="row_one_icon map" src="src/images/map1.png"/><img class="row_one_icon paper_stack" id="paper" src="src/images/paper_stack_Off.png" onclick="PaperStack()"/><img class="row_one_icon chain" id="chain" src="src/images/chain_Off.png" onClick="ChainLink()"/></span>'; html2 += '<span class="details">Site name / manufacturer</span>'; html2 += '<span class="details">Selling Sales Eng</span>'; html2 += '</div>' html2 += '</div>'; if (!document.getElementById('map_tooltip')) { tooltipdiv = $("<div></div>").attr('id', "map_tooltip"); $(document.body).append(tooltipdiv); $(tooltipdiv).css({ "display": "none", "padding": "5px", "position": "absolute", "z-index": "13000", "cursor": "default", "font-family": "Segoe UI", "color": "#707070", "font-size": "12px", "pointer-events": "none", "background-color": "#FFFFFF", "border": "1px solid #707070" }); } else { tooltipdiv = $("#map_tooltip"); $(tooltipdiv).css({ "left": (e.pageX + 5), "top": (e.pageY + 5) }); $(tooltipdiv).html(html).show("slow"); //$('#defaulttext').hide(); //$('#searchcontainer').append(html2); //$('.rightcontainer').eq($(this).index()).addClass('background'); $(html2).appendTo("#searchcontainer"); }
Now in this code I was able to display my result on a div by hovering on a marker on the map but if I return on my previous hovered marker instead of highlighting its corresponding result, it will just make another rendering of the same result below in which I dont need
原文:https://stackoverflow.com/questions/46701962
最满意答案
你可以试试
entries <- unlist(strsplit(text, "\\n")) #separate entries by line breaks entries <- entries[nchar(entries) > 0] #remove empty lines as.data.frame(matrix(entries, ncol=3, byrow=TRUE)) #assemble dataframe # V1 V2 V3 #1 DOB Name Address #2 13-03-2003 ABC xyz. #3 12-08-2004 dfs 1 infinite loop.
数据
text <-'DOB Name Address 13-03-2003 ABC xyz. 12-08-2004 dfs 1 infinite loop.' df <- read.table(text = text)
You could try
entries <- unlist(strsplit(text, "\\n")) #separate entries by line breaks entries <- entries[nchar(entries) > 0] #remove empty lines as.data.frame(matrix(entries, ncol=3, byrow=TRUE)) #assemble dataframe # V1 V2 V3 #1 DOB Name Address #2 13-03-2003 ABC xyz. #3 12-08-2004 dfs 1 infinite loop.
data
text <-'DOB Name Address 13-03-2003 ABC xyz. 12-08-2004 dfs 1 infinite loop.' df <- read.table(text = text)
相关问答
更多-
尝试read.table(comment.char="?" ...) ,你用作comment.char的东西不在你的表中: read.table( comment.char="?", header=T, check.names=F, text="ColA ColB# ColC ColD ColE\n1 2 3 4 5" ) # ColA ColB# ColC ColD ColE # 1 1 2 3 4 5 try read.table(comment.char= ...
-
使用fopen打开txt文件: $handle = @fopen("xyz.txt", "r"); //read line one by one $values=''; while (!feof($handle)) // Loop til end of file. { $buffer = fgets($handle, 4096); // Read a line. list($a,$b,$c)=explode("|",$buffer);//Separate string by the mea ...
-
鉴于表格是唯一以数字开头的元素,所以在行首开始处理整数确实是最好的解决方案。 使用shell(而不是R)命令: grep '^[0-9]' input > output 做了我想要的东西。 Given that the tables are the only elements that start with numbers, to grep for integers at the beginning of the line is indeed the best solution. Using the sh ...
-
我想你可以简化你的代码,因为你想要20个块。另外,我认为你需要追加结果,否则你会在每个循环中覆盖你的代码。 尝试这个: DF <- data.frame(C1=sample(1:1000,200), C2=sample(1:1000,200)) outfile<- file('outfile.txt') for(i in seq(1,200, 20)){ for(j in seq(20,200, 20)){ g <- as.character(diameter(graph.data.frame ...
-
你可以试试 entries <- unlist(strsplit(text, "\\n")) #separate entries by line breaks entries <- entries[nchar(entries) > 0] #remove empty lines as.data.frame(matrix(entries, ncol=3, byrow=TRUE)) #assemble dataframe # V1 V2 V3 #1 ...
-
在R中读取文本文件(Reading a text file in R)[2022-06-19]
您可以指定一个向量,其中包含您要读取的列的类。 例如,如果您的源数据有两列,第一列是您想要的数字,第二列是因子,那么您可以使用: classes <- c(col1="numeric", col2="factor") read.table("file.text", colClasses=classes, stringsAsFactors=FALSE) You can specify a vector which contains the classes of the columns which you i ... -
避免覆盖R中的文本文件(Avoid overwriting a text file in R)[2023-05-31]
sink()有自己的append参数。 正如Gregor所提到的,在cat() append仅在使用file有效。 但是,如果将所有cat()调用放在sink()调用之间,则根本不需要使用append ,因为sink()将继续附加到文件,直到调用sink(NULL) 但是对于你的情况,我认为你想为你的sink()块做这样的事情: sink("output.txt", type=c("output", "message"), append = TRUE) cat("Hello", "\n") cat("Wor ... -
正如Pop所提到的,你需要创建一个列名列表并循环遍历这些列名。 lapply为for循环提供了一种更优雅的替代方案。 通过更清楚地布置代码,您可以看到在分配result1和result2的行中有一些奇数双括号。 考虑到将这些线分解为较小的计算以保持清晰 columns <- paste0("X", 1:14) lapply( columns, function(column) { result1 <- ( mean(ref[[column]]) - ((sd(r ...
-
set.seed(1000) df = data.frame(a=c(9999999,sample(2:78,77,replace = F))) plot(x=1:nrow(df), y=log(df$a)) i)set.seed(1000)可以帮助您在每次运行此代码时从样本()中重现相同的随机数。 它使代码具有可重现性。 ii)在R控制台中键入?样本以获得文档。 iii)因为你想要x轴是亚麻布 - 我用“:”运算符创建它。 1:3 = 1,2,3。 类似地,我使用1:nrow(df)创建了一个“ ...
-
在read.table函数中已经传递header = TRUE时,不需要再次指定列名。 colClasses参数将处理列数据的类。 df <- read.table(file.choose(), header = TRUE, sep = ",", colClasses = c('character', 'character'), na.strings = 'null') # convert character to numeric format char_cols <- which(sapply(df, ...