简体   繁体   English

JavaScript保留关键字未正确通过函数传递

[英]JavaScript reserved keywords not correctly being passed through function

I'm attempting to build a page where a user can mark cities on a map that they have visited, storing the data on Firebase for future visits. 我正在尝试建立一个页面,用户可以在该页面上标记他们访问过的地图上的城市,并将数据存储在Firebase上以备将来访问。 The issue I've run into is with cities such as "New York" or "New Orleans" that begin with reserved keywords. 我遇到的问题是诸如“纽约”或“新奥尔良”之类的以保留关键字开头的城市。 This is the function that runs on page load to adjust previously visited cities to be marked on the map as visited with a checkmark image. 此功能在页面加载时运行,以调整以前访问过的城市,以在地图上标记为带有对勾图像的访问过的城市。

  function adjustMarkers() {
  database.ref("users/" + uid + "/citiesVisited").once("value").then(function(userSnapshot){
    userSnap = userSnapshot.val();
    citiesArray = Object.entries(userSnap);
    for (var [cityName, visited] of citiesArray) {
        if (visited == "yes") {
            $("#" + cityName).attr("src", "assets/images/checkmark.png")
            $("#" + cityName).attr("data-clicked", "yes")
        }
    }
})

} }

When this function runs, cities like New York, New Orleans, Washington DC, and Los Angeles do not get correctly updated. 运行此功能时,纽约,新奥尔良,华盛顿特区和洛杉矶等城市无法正确更新。 I realize that I can store these city names in Firebase as other names like NY, NO, DC, and LA to get this to run properly. 我意识到我可以将这些城市名称存储在Firebase中,作为其他名称,例如NY,NO,DC和LA,以使其正常运行。 But I would like very much to keep them stored with their proper names for other display purposes. 但是我非常想保留它们的专有名称用于其他显示目的。 Is there a workaround I can use on this side of things? 我可以在这方面使用替代方法吗?

First post here, I apologize if this post breaks any rules. 在这里的第一篇文章,如果这篇文章违反任何规则,我深表歉意。 I searched a fair amount before posting this but may have missed a similar previous question. 在发布此消息之前,我搜索了很多内容,但是可能错过了类似的先前问题。

Edit: On further thinking, this might not have anything to do with Reserved Words at all, and may just be all cities that have multiple words. 编辑:进一步思考,这可能与保留字根本无关,可能只是所有有多个字的城市。 I will delete this if the solution becomes immediately apparent to me. 如果解决方案对我而言立即显而易见,我将删除此内容。

The problem is not with Javascript reserved keywords - the problem is that, if cityName has spaces, then, for example, if it's New York : 问题不在于Javascript保留关键字-问题是,如果cityName有空格,例如,如果是New York

$("#" + cityName)

will resolve to 将解决

$("#New York")

which means: find an element whose tagName is "York" which is a descendant of an element whose ID is "New". 这意味着:查找其tagName为“ York”的元素,该元素为ID为“ New”的元素的后代。

 $("#New York").text('foo'); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="New"> <York> ???? </York> </div> 

Any cityName with spaces will not function as expected. 任何带有空格的cityName无法正常运行。

An easy fix would be to replace all spaces with dashes, in the HTML (and either in the database as well, or before passing cityName to $ ), so that it resolves to something like 一个简单的解决方法是在HTML中(以及在数据库中,或者在将cityName传递给$之前)用破折号替换所有空格,以便将其解析为类似

$("#New-York")

If a database contains data that includes Javascript reserved words, probably the only time you'd have to worry about that would be if you're using eval or one of its equivalents - it shouldn't ever be something to worry about otherwise, I think. 如果数据库中包含包含Javascript保留字的数据,那么您可能唯一需要担心的就是使用eval或其等效项之一-否则就不必担心了,我认为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM