[英]Find Parent Sibling First Child's First Child using Jquery
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.当单击按钮时,我正在尝试在此处使用 JS 和 Jquery 的组合来选择输入文本(之前的几个元素)。
I know how to do this just using JS but i want to understand how to do this using jQuery.我知道如何仅使用 JS 来做到这一点,但我想了解如何使用 jQuery 来做到这一点。 I get the error message in console: TypeError: ele.setSelectionRange is not a function.
我在控制台中收到错误消息: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.我在这里没有使用 ID 或 Class 来识别输入。
Can someone help me here?有人可以在这里帮助我吗? Thanks
谢谢
JS 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 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>
setSelectionRange(0, 99999)
is not a method on jQuery object. setSelectionRange(0, 99999)
不是 jQuery 对象的方法。 Use it on DOM element.在 DOM 元素上使用它。
So try: [0]
所以试试:
[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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.