有人可以引导我走这条线吗

有人可以引导我走这条线吗

问题描述:

var types = {
  "Grocery": "gro",
  "Restaurant": "res",
  "Bar": "bar",
  "Pizza Delivery": "piz",
  "Quick Service": "qui",
  "Retail": "ret",
  "Salon": "sal"
}

$(".type_changer").attr("id", types[$(this).text()]);

我了解到type_changer类ID已更改为该数组的一部分,但我不理解types[$(this).text()]

I understand that the class type_changer id is being changed to the part of this array but I don't understand types[$(this).text()]

和这行

$(this).parents('.select-holder').find('.text').text($(this).text());

似乎几乎可以理解,但是我对.parents.find('.text').text($(this).text());

seems almost understandable but I get confused on the .parents and the .find('.text').text($(this).text());

$(this).text()接受当前元素的文本(在您的情况下,您正在循环浏览的<li>,除非您的标记已更改).

The $(this).text() takes the text of the current element (the <li> you're looping over in your case, unless your markup has changed).

然后,它将文本用作types对象上的键,例如,当您单击杂货"链接时,基本上执行types["Grocery"].

It then uses that text as the key on the types object, basically doing types["Grocery"] when you click the "Grocery" link for example.

在JavaScript中,您可以执行types.Grocerytypes["Grocery"]来访问值为"gro"的属性.

In JavaScript you can do types.Grocery or types["Grocery"] to access the property, which has a value of "gro".

最后一条语句正在进行相同的杂货"测试,并将其设置为父级中class="text"元素的文本.

The last statement is taking that same "Grocery" test and setting it as the text for the class="text" element in the parent.