首页 \ 问答 \ 避免使用JavaScript或jQuery重复结果(avoid repeating of result using JavaScript or jQuery)

避免使用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
更新时间:2022-05-22 09:05

最满意答案

你可以试试

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 ...
  • 您可以指定一个向量,其中包含您要读取的列的类。 例如,如果您的源数据有两列,第一列是您想要的数字,第二列是因子,那么您可以使用: 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 ...
  • 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, ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)