【发布时间】:2023-04-21 21:54:01
【问题描述】:
如何通过类和段落id获取span的值??
代码
function doit_onkeypress(a) {
debugger;
if (event.keyCode == 13 || event.which == 13) {
alert(a);
var splitdata;
span_array = [];//define array
$(".token").each(function () {// iterate over same class spans
console.log($(this).text()); // print the text of each span
span_array.push($(this).text());// push span text to array
});
console.log(span_array); // you can save data an array for further use
var final_string = span_array.join(); //join array value as string
console.log(final_string); // check string
splitdata = final_string
document.getElementById("hdnvaluearray").value = final_string;
document.getElementById("hdnvaluearray").value = a;
}
}
test1
test2
test3
test4
test5
test6
此代码向我显示跨度值,例如 test1、test2、test3、test4、test5、test6 但我想显示这些跨度值 (当点击第一段显示输出)像 test1,test2,test3 (当点击第二段显示输出时)like test4,test5,test6
-
你真正想要什么?一个数组还是只是 p 中的文本?
-
我想要焦点数组
@pete
-
好的,第二个问题 - p 内容不可编辑,所以您希望如何在其中捕获按键事件?你的意思是你想让这个事件触发 p 的 onclick 吗?
-
实际上我正在使用这个函数来生成令牌,所以这里也包含一个输入标签,但我没有在这个问题中显示,我认为这没有必要
-
获取数组最简单的方法,但是是Jquery是: $(document).on("click", "#para", function (){var array= $(".token" ).map(function () { return $(this).text() }).toArray();});