查找父母兄弟姐妹的第一个孩子使用jQuery的第一个孩子

发布于 2025-02-03 23:10:09 字数 1062 浏览 5 评论 0原文

我正在尝试在此处使用JS和jQuery的组合来选择输入文本(以前的几个元素),当单击按钮时。

我知道如何使用JS执行此操作,但我想了解如何使用jQuery做到这一点。我在控制台中获取错误消息:typeError:ele.setselectionrange不是一个函数。我认为这意味着它没有按照我需要的方式定义输入值。

我在这里不使用ID或类来识别输入。

有人可以在这里帮我吗?谢谢

JS

$(document).ready(function () {
  $('.jimmy').click(function(e){
  e.preventDefault;
  jimsFunction(this);
  });
});

function jimsFunction(input) {
  let ele = $(input).parent().siblings(':first-child').children();
  ele.select();
  ele.setSelectionRange(0, 99999);
  navigator.clipboard.writeText(ele.value);
  alert("Copied: " + ele.value);
}

HTML

      <div class="colbody">
        <div>
          <input type="text" value="www.brave.com" readonly>
        </div>
        <div>
          <a href="www.google.com" target="_blank">View</a>                                
        </div>
        <div>
          <button type="button" class="jimmy">Copy URL</button>
        </div>
      </div>  

I'm trying to Use a combination of JS and Jquery here to select the input text (a few elements previous), when the button is clicked.

I know how to do this just using JS but i want to understand how to do this using jQuery. I get the error message in console: TypeError: ele.setSelectionRange is not a function. Which I take it means that it is not defining the Input Value the way I need it to.

I'm not using ID or Class here to identify the input.

Can someone help me here? Thanks

JS

$(document).ready(function () {
  $('.jimmy').click(function(e){
  e.preventDefault;
  jimsFunction(this);
  });
});

function jimsFunction(input) {
  let ele = $(input).parent().siblings(':first-child').children();
  ele.select();
  ele.setSelectionRange(0, 99999);
  navigator.clipboard.writeText(ele.value);
  alert("Copied: " + ele.value);
}

HTML

      <div class="colbody">
        <div>
          <input type="text" value="www.brave.com" readonly>
        </div>
        <div>
          <a href="www.google.com" target="_blank">View</a>                                
        </div>
        <div>
          <button type="button" class="jimmy">Copy URL</button>
        </div>
      </div>  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

時窥 2025-02-10 23:10:09

setSelectionRange(0,99999)不是jQuery对象上的方法。在DOM元素上使用它。

因此,尝试:[0]

示例:

$('.jimmy').click(function(e) {
  e.preventDefault;
  jimsFunction(this);
});


function jimsFunction(input) {
  let ele = $(input).parent().siblings(':first-child').children();
  ele[0].select();
  ele[0].setSelectionRange(0, 99999);
  navigator.clipboard.writeText(ele[0].value);
  alert("Copied: " + ele[0].value);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="colbody">
  <div>
    <input type="text" value="www.brave.com" readonly>
  </div>
  <div>
    <a href="www.google.com" target="_blank">View</a>
  </div>
  <div>
    <button type="button" class="jimmy">Copy URL</button>
  </div>
</div>

setSelectionRange(0, 99999) is not a method on jQuery object. Use it on DOM element.

So try: [0]

Example:

$('.jimmy').click(function(e) {
  e.preventDefault;
  jimsFunction(this);
});


function jimsFunction(input) {
  let ele = $(input).parent().siblings(':first-child').children();
  ele[0].select();
  ele[0].setSelectionRange(0, 99999);
  navigator.clipboard.writeText(ele[0].value);
  alert("Copied: " + ele[0].value);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="colbody">
  <div>
    <input type="text" value="www.brave.com" readonly>
  </div>
  <div>
    <a href="www.google.com" target="_blank">View</a>
  </div>
  <div>
    <button type="button" class="jimmy">Copy URL</button>
  </div>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文