查找父母兄弟姐妹的第一个孩子使用jQuery的第一个孩子
我正在尝试在此处使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
setSelectionRange(0,99999)
不是jQuery对象上的方法。在DOM元素上使用它。因此,尝试:
[0]
示例:
setSelectionRange(0, 99999)
is not a method on jQuery object. Use it on DOM element.So try:
[0]
Example: