获取JavaScript中的值

获取JavaScript中的值,javascript,jquery,html,Javascript,Jquery,Html,如何使用类和段落id获取跨度值 代码 功能按键(a){ 调试器; if(event.keyCode==13 | | event.which==13){ 警报(a); var数据; span_array=[];//定义数组 $(“.token”)。每个(函数(){//在相同的类跨度上迭代 console.log($(this.text());//打印每个跨度的文本 span_array.push($(this.text());//将span文本推送到数组 }); console.log(span

如何使用类和段落id获取跨度值

代码

功能按键(a){
调试器;
if(event.keyCode==13 | | event.which==13){
警报(a);
var数据;
span_array=[];//定义数组
$(“.token”)。每个(函数(){//在相同的类跨度上迭代
console.log($(this.text());//打印每个跨度的文本
span_array.push($(this.text());//将span文本推送到数组
});
console.log(span_array);//您可以将数据保存到数组中以供进一步使用
var final_string=span_array.join();//将数组值作为字符串连接
console.log(最终_字符串);//检查字符串
splitdata=最终字符串
document.getElementById(“hdnvaluearray”).value=final_字符串;
document.getElementById(“hdnvaluearray”).value=a;
}
}

测试1 测试2 测试3

测试4 测试5 测试6

要修复代码:

  • 添加
    事件

    function doit_onkeypress(a, event) {
    

  • 在段落中添加一个tabindex,使其能够集中

  • 这就是我的工作原理:

    <html>
    <head>
    
    </head>
    <body>
    <p id="para" tabindex="0" onkeypress="doit_onkeypress(this.id, event);">
        <span class="token">test1</span>
        <span class="token">test2</span>
        <span class="token">test3</span>
    </p>
    
    <p id="para2" tabindex="1" onkeypress="doit_onkeypress(this.id, event);">
        <span class="token">test4</span>
        <span class="token">test5</span>
        <span class="token">test6</span>
    </p>
    <script>
    function doit_onkeypress(a, event) {
        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;
        }
    }
    </script>
    </body>
    </html>
    
    
    

    测试1 测试2 测试3

    测试4 测试5 测试6

    按键功能(a,事件){ 调试器; if(event.keyCode==13 | | event.which==13){ 警报(a); var数据; span_array=[];//定义数组 $(“.token”)。每个(函数(){//在相同的类跨度上迭代 console.log($(this.text());//打印每个跨度的文本 span_array.push($(this.text());//将span文本推送到数组 }); console.log(span_array);//您可以将数据保存到数组中以供进一步使用 var final_string=span_array.join();//将数组值作为字符串连接 console.log(最终_字符串);//检查字符串 splitdata=最终字符串 document.getElementById(“hdnvaluearray”).value=final_字符串; document.getElementById(“hdnvaluearray”).value=a; } }
    更换此部件

    $(“.tknz令牌标签”)

    $(“#段落标记”)。每个(函数(){
    });

    你到底想要什么?一个数组还是仅仅是p中的文本?我想要焦点数组@peteOk所以第二个问题-p的内容是不可编辑的,所以你希望如何捕获其中的按键事件?你的意思是想让这个事件在点击p时触发吗?实际上,我正在使用这个函数来生成令牌,所以这里也包含了一个输入标记,但我没有在这个问题中显示,我认为这不是必要的。获取数组的最简单方法,但是Jquery是:$(document)(函数(){return$(this.text()}).toArray();});感谢您的回答,它对

    正常工作,但在单击时不会生成事件

    我编辑了我的答案。

    $("#" + a + ".token").each(function () {
    
    <html>
    <head>
    
    </head>
    <body>
    <p id="para" tabindex="0" onkeypress="doit_onkeypress(this.id, event);">
        <span class="token">test1</span>
        <span class="token">test2</span>
        <span class="token">test3</span>
    </p>
    
    <p id="para2" tabindex="1" onkeypress="doit_onkeypress(this.id, event);">
        <span class="token">test4</span>
        <span class="token">test5</span>
        <span class="token">test6</span>
    </p>
    <script>
    function doit_onkeypress(a, event) {
        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;
        }
    }
    </script>
    </body>
    </html>