JS DOM programming Art second Edition Chapter 8 abbreviations problem

https://codepen.io/deepblue19...

Source code adopts the tag ,

traversing first.

then store the acquired data in an "associative array",

(the text node of does key, and the value of an attribute title of does the value of the array)

I don"t understand why it is necessary to set up an array and then take values from the array. It is very existent to save first and then fetch it.

so I didn"t use the method in the book, just like this:

var abbr = document.getElementsByTagName("abbr");
if (abbr.length < 1) return false;
var dlist = document.createElement("dl");
for (var i = 0; i < abbr.length; iPP) {
    var definition = abbr[i].getAttribute("title");
    var text = abbr[i].innerText;

    var dtitle = document.createElement("dt");
    var dtitle_text = document.createTextNode(definition);
    dtitle.appendChild(dtitle_text);

    var ddesc = document.createElement("dd");
    var ddesc_text = document.createTextNode(text);
    ddesc.appendChild(ddesc_text);

    dlist.appendChild(ddesc);
    dlist.appendChild(dtitle);
   }
document.body.appendChild(dlist);

with some IDE (HBuilder) built-in browser, it works, but neither chrome nor firefox can pass.

check for errors with Jslinter. Hint:

if (abbr.length < 1) return false;
>> index.js: "return" outside of function (2:23)

I am a rookie, and I don"t know how to do it, so I delete this item.

then, everything OK ~

WHY????

Apr.14,2022

return can only be written in functions


first of all, there is no problem with the code you write. The reason for reporting an error is that jslint has strict requirements on format. This shows that you delete this line and no longer report an error. The abbr.length in your code is greater than 1, so the role of this line of code can be ignored. After you delete it, you will ok

.

as for why the author creates an array to store the data, it may be for other purposes, such as applying the data again without having to execute the function and applying the array directly to ok.

Menu