SelectablejQuery移动替代

问题描述

我一直在寻找API的解决方案,但找不到。所有示例或建议均无效。有人可以帮我吗?还是给我任何建议?我仍在学习JQuery,所以任何帮助都将受到欢迎。

HTML:

<!DOCTYPE html>
<html lang="en">

<head>
    <Meta charset="UTF-8">
    <Meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>New api</title>
    <link rel="stylesheet" href="./style.css">
</head>
<body>
    <main>
        <section>
            <div id="alert"></div>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script>
            <ul id="wordblock">
            </ul>
            <div id="result">Result</div>
        </section>
        <script src="./api.js"></script>
    </main>
</body>

</html>

jQuery代码

function screenResolutionAlert(x) {
    if (x.matches) {
        $("#alert").html("This API doesn't work with touchpads <br> (mobiles,tablets etc) <br> please use computer or laptop with a mouse").show();
    } else {
        $("#alert").hide();
    }
}
var x = window.matchMedia("(max-width: 1200px)")
screenResolutionAlert(x)
x.addListener(screenResolutionAlert)

//API swap words code
$(function () {
    $("#wordblock").sortable();
    $("#wordblock").disableSelection();
    const array = ["pierogi","gołąbki","pies","sześcian"];
    const word = array[Math.floor(Math.random() * array.length)];
    let d_word = word.split('');
    shuffle(d_word);

    const lis = [];
    for (let i = 0; i < d_word.length; i++) {
        lis.push('<li class="ui-state-default">' + d_word[i] + '</li>')
    }

    $('#wordblock').html(lis.join(''));

    $('#wordblock').mouseup(function () {
        setTimeout(() => {
            let r_word = '';
            $('#wordblock>li').each(function (e) {
                r_word += $(this).text();
            });
            if (r_word == word) {
                $("#result").html(`Correct! It was exactly "${r_word}"`);
            } else {
                $("#result").html(`Wrong! keep trying.. <br> it's not "${r_word}"`);
            }
        },0);
    });

});

function shuffle(a,b,c,d) {
    c = a.length;
    while (c) b = Math.random() * (--c + 1) | 0,d = a[c],a[c] = a[b],a[b] = d
}

是的,我正在使用移动Jquery链接,但没有用...以及任何版本的..我都尝试了互联网上写的所有内容;(

解决方法

我尝试了您的代码,但...似乎有效!代码段位于下面,只需按运行代码段,然后将字母排序为“ PIES”即可。

我建议您阅读有关API的信息,因为目前您根本不使用任何API! ?

function screenResolutionAlert(x) {
  if (x.matches) {
    $("#alert")
      .html(
        "This API doesn't work with touchpads <br> (mobiles,tablets etc) <br> please use computer or laptop with a mouse"
      )
      .show();
  } else {
    $("#alert").hide();
  }
}
var x = window.matchMedia("(max-width: 1200px)");
screenResolutionAlert(x);
x.addListener(screenResolutionAlert);

//API swap words code
$(function () {
  $("#wordblock").sortable();
  $("#wordblock").disableSelection();
  const array = ["pies"];
  const word = array[Math.floor(Math.random() * array.length)];
  let d_word = word.split("");
  shuffle(d_word);

  const lis = [];
  for (let i = 0; i < d_word.length; i++) {
    lis.push('<li class="ui-state-default">' + d_word[i] + "</li>");
  }

  $("#wordblock").html(lis.join(""));

  $("#wordblock").mouseup(function () {
    setTimeout(() => {
      let r_word = "";
      $("#wordblock>li").each(function (e) {
        r_word += $(this).text();
      });
      if (r_word == word) {
        $("#result").html(`Correct! It was exactly "${r_word}"`);
      } else {
        $("#result").html(`Wrong! keep trying.. <br> it's not "${r_word}"`);
      }
    },0);
  });
});

function shuffle(a,b,c,d) {
  c = a.length;
  while (c)
    (b = (Math.random() * (--c + 1)) | 0),(d = a[c]),(a[c] = a[b]),(a[b] = d);
}
ul#wordblock {
  padding-left: 0;
}

ul#wordblock li {
  display: inline-block;
  font-size: 2em;
  padding: 0.2em 0.2em;
  cursor: pointer;
  background-color: aliceblue;
  border-radius: 50%;
  margin: 0.5em;
  width: 1em;
  height: 1em;
  text-align: center;
  line-height: 0.9em;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width,initial-scale=1.0">
  <title>New api</title>
  <link rel="stylesheet" href="./style.css">
</head>

<body>
  <main>
    <section>
      <div id="alert"></div>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script>
      <ul id="wordblock">
      </ul>
      <div id="result">Result</div>
    </section>
    <script src="./api.js"></script>
  </main>
</body>

</html>