Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...