jQuery 函数怎么修改成传递数组?
chrome.extension.onMessage.addListener(function(request,sender,sendResponse){if(reque...
chrome.extension.onMessage.addListener( function (request, sender, sendResponse) { if (request.action === "copy") { var ctrl = $("#sb_form_q"); if (ctrl.length > 0) { if (sendResponse) sendResponse(ctrl.val()); } else { alert("No data"); } } else if (request.action === "paste") { var ctrl = $("#input"); if (ctrl.length > 0) { ctrl.val(request.data); sendResponse("OK"); } else { alert("No data"); } } });现在实现的是复制A网站id="sb_form_q"的值,然后粘帖到B网站id="input"里面。一对一。我的问题是如果要从A网站到B网站传递的数组要怎么改?谢谢了。
展开
1个回答
展开全部
你要改数组什么东西。数组结构是什么样的?
网页链接 这里有操作数组的所有的方法
追问
现在我复制的是id="sb_form_q1"的值,然后粘帖到id="input1"里面。
我想要的结果是可以复制id="sb_form_q1",id="sb_form_q2",id="sb_form_q3"。。
到对应的id="input1",id="input2",id="input3"里面。
我自己想的是要组成一个数组通过点击paste传递,但不知道怎么写。。
追答
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
</head>
<body>
<div class="not">dsad</div>
<div class="not">dsad</div>
<div class="not">dsad</div>
<div id="sb_form_q1">sb_form_q1</div>
<div id="sb_form_q2">sb_form_q2</div>
<div class="not">xzklxjksakjjk</div>
<div id="sb_form_q3">sb_form_q3</div>
<div class="not">dsakjdasjkd</div>
<div id="sb_form_q4">sb_form_q4</div>
<button id="action">sb_form值到对应的input里</button>
<script async>
$(document).ready(function() {
forms = $('[id^="sb_form_q"]') // 取得id值 开头为sb_form_q所有节点
$("#action").click(function() { // 给button 添加click事件
$.each(forms, function(index, item) {
// jquery each方法 index参数代表当前索引,item则是数组里的html节点
// 具体: http://www.w3school.com.cn/jquery/traversing_each.asp
var _item = '<div id="input' + (index + 1) + '">input' + (index + 1) + '=' + $(item).text() + '</div>'
// each出来的item是html节点对象,用$(item)将他转成jquery对象,用jquery的text()方法获取到里面的字符串
$('body').append(_item)
})
})
})
</script>
</body>
</html>
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询