Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

How to start debugging jQuery if there seems to be no error?

I have a script to swap text back and forth:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
var words = [];
words.push('vocabulary');
words.push('lexicon');
words.push('lexicons');
</script>

<p id="demo">A vocabulary is a list of words that an individual knows or uses regularly. vocabulary is different from lexicon because vocabulary is about what an individual or group of people know, whereas lexicon is about the language itself. In this paragraph, lexicons is a new word that's added, so don't forget to push 'lexicons' in your array.</p>

<script>
function toggle(element) {
  if (element.innerHTML.split('_').join('').trim().length === 0) {
    element.innerHTML = element.getAttribute("word");
  } else {
    element.innerHTML = "_______";
  }
}

$.each(words, function(index, value) {
  var replacestr = new RegExp('\b'+value+'\b', "g");
  $("p#demo:contains('" + value + "')").html(function(_, html) {
    return html.replace(replacestr, ' <span class = "smallcaps" word="' + value + '" onclick="toggle(this)">_______</span>')
  });
});
</script>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your code doesn't appear to be included on site 2 so it is never run. On site 1 I can see the scripts that initiate the toggling...

First is the word array builder which is found in the <head> tag. enter image description here

Second is the toggle function and initiator. enter image description here

Both of which do not appear to be included in site 2.


UPDATE

As mentioned in the comments, your script is actually included however, your paragraph contains none of the words in your array.

This is your paragraph: enter image description here

And this is your words array: enter image description here

Your script is searching the above paragraph for: vocabulary, lexicon and lexicons all of which are not in the content.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...