jquery 如何当点击一个单词时,单词的元素可以改变让单词保持不变

nr9pn0ug  于 2023-06-29  发布在  jQuery
关注(0)|答案(1)|浏览(110)
var words = $( "p" ).first().text().split( /\\s+/ );

var text = words.join( "\</span\> \<span\>" );

$( "p" ).first().html( "\<span\>" + text + "\</span\>" );

$( "span" ).on( "click", function() {

$( this ).css( "background-color", "red" );

});

<!doctype html>
<html lang="en">
<head>
<script src="https://code.jquery.com/jquery-git.js"></script>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>rebaz</title>
<link rel="stylesheet" href="style.css" />
</head>

<body class = "card" >

<p  >This domain is established to be used for illustrative examples in documents. You may use this domain in examples without prior coordination or asking for permission.
</p>



<div  class="custom-context-menu">   <ul>

 <a><li id="html" >rule</li> </a></ul>
<ul> <a><li id="html" >slde</li> </a></ul>
<ul> <a><li id="html" >rebaz</li> </a></ul></div>


<script>
var words = $( "p" ).text().split( /\s+/ );
var text = words.join( "</span> <span>" );
$( "p" ).first().html( "<span>" + text + "</span>" );
$( "span" ).on( "click", function() {

$( this ).css( "background-color", "red" );
});

var card = document.querySelector("span");

var customContextMenu = document.querySelector(".custom-context-menu");

card.addEventListener("touchend", (e) => {
  e.preventDefault();
  let topPosition = e.clientY;
  let leftPosition = e.clientX;
  customContextMenu.classList.add("active");

  customContextMenu.style.left = leftPosition + "px";
  customContextMenu.style.top = topPosition + "px";
});

window.addEventListener("click", () => {
  customContextMenu.classList.remove("active");
});
</script>

</body>
</html>
xxb16uws

xxb16uws1#

像这样?
为了关闭上下文菜单,我使用了一个背景,这是一个div,它覆盖了上下文菜单之外的所有内容,当它被点击时,它会关闭自己和上下文菜单,你尝试的方式,点击窗口不会工作,因为点击一个单词会触发它,上下文菜单会同时打开和关闭。
同样,当你使用jquery时,我把所有东西都转换成了jquery,监听器和选择器。

<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="https://code.jquery.com/jquery-git.js"></script>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>rebaz</title>

    <style>
      .custom-context-menu {
        z-index: 1100;
        display: none;
        position: absolute;
        list-style: none;
        margin: 0;
        padding: 0;
        border: 1px solid black;
        background: white;
      }

      .custom-context-menu li {
        padding: 5px;
        cursor: pointer;
      }

      .custom-context-menu li:hover {
        background-color: aqua;
      }

      .backdrop {
        display: none;
        position: absolute;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
        z-index: 1000;
      }
    </style>
  </head>

  <body class="card">
    <p>
      This domain is established to be used for illustrative examples in documents. You may use this domain in examples
      without prior coordination or asking for permission.
    </p>

    <ul class="custom-context-menu">
      <li>rule</li>
      <li>slde</li>
      <li>rebaz</li>
    </ul>

    <div class="backdrop"></div>

    <script>
      const words = $("p").text().split(/\s+/);
      const text = words.join("</span> <span>");

      let incorrectWord = null;

      $("p")
        .first()
        .html("<span>" + text + "</span>");

      const customContextMenu = $(".custom-context-menu");
      const backdrop = $(".backdrop");

      $("span").on("click", function (e) {
        let topPosition = e.clientY;
        let leftPosition = e.clientX;
        customContextMenu.show();
        backdrop.show();

        incorrectWord = $(e.target);

        customContextMenu.css("left", leftPosition + "px");
        customContextMenu.css("top", topPosition + "px");
      });

      customContextMenu.find("li").on("click", function () {
        const chosenWord = $(this).html();
        incorrectWord.html(chosenWord);
        customContextMenu.hide();
        backdrop.hide();
      });

      backdrop.on("click", () => {
        customContextMenu.hide();
        backdrop.hide();
      });
    </script>
  </body>
</html>

相关问题