Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在元素后查找文本和/或标记?(Jquery,JS)_Javascript_Jquery - Fatal编程技术网

Javascript 如何在元素后查找文本和/或标记?(Jquery,JS)

Javascript 如何在元素后查找文本和/或标记?(Jquery,JS),javascript,jquery,Javascript,Jquery,我使用的是Jquery 3.4.1 我有一个html表单,其中包含如下元素。这是放置 <section id=question1> <input name=A type=radio value=1>Some text (Some text) <input name=A type=radio value=2><i>Some text</i> <u class=somewrapper><input name=A type=

我使用的是Jquery 3.4.1

我有一个html表单,其中包含如下元素。这是放置

<section id=question1>
<input name=A type=radio value=1>Some text (Some text)
<input name=A type=radio value=2><i>Some text</i>
<u class=somewrapper><input name=A type=radio value=3><i>Some text<i> (Some text)</u>
<u class=somewrapper><input name=A type=radio value=4><i><em>Some text</em><i> (Some text)</u>
<u class=somewrapper><input name=A type=radio value=5><u><em>Some text</em><u> (Some text)</u>
</section>
我需要得到的东西是它的标签或空文本之间的选择输入和下一个输入或本节结束

当第一次选择输入时,结果可能是数组[0]=一些文本一些文本 选择第二个输入时,结果可能是数组[0]=对象,带有包含文本的i标记。 当选择第三个输入时,结果可能类似于数组[0]=带有包含文本的i标记的对象Some text和数组[1]=带有文本的对象Some text或类似的内容

我还尝试获得以下要素:

console.log( $(Chk).NextAll() );
但是我在Chrome49.0.2623.112M中得到了错误,NextAll不是一个函数

注:
我之所以要在输入后搜索文本,是因为我想在移动设备的用户单击radio box时修改文本。我想在选择该选项后隐藏文本或使其变短,并使显示的文本在小屏幕上占用更少的空间。这不是讨论的主题,我只是想解释一下为什么我要查找后跟输入标记的文本。

用类似的类标记与输入元素关联的文本怎么样

在这里使用css选择器非常困难,因为布局是多么不一致

然后使用:

$("input[type='radio']").on("click", function(){
   let val = this.value;
   let allText = $(`.label-${val}`);
   let trimmedText = [];
   for(let text of allText) {
    trimmedText.push($(text).html().trim());
   }
   let concatText = trimmedText.join(' ');
   console.log(concatText)
});

请参阅:

理想情况下,您应该具有一致的HTML结构,这样会更容易。但是,您可以尝试以下方法:

$("input[type='radio']").on("click", function() {
    var outputText = [];
    var current = this.nextSibling;
    while (current !== null && current.tagName !== "INPUT") {
        if (current.tagName) { //Check if element or node
            outputText.push(current.innerText)
        } else {
            outputText.push(current.nodeValue)
        }
        current = current.nextSibling;
    }
});

一个限制是,对于某个元素(如某些文本、某些文本),它会在扫描该元素以及扫描节点时添加一些文本和某些文本,从而导致重复。您可以通过检查当前元素是否包含子元素并处理子元素来进一步优化它。

NextAll应该是NextAll。Javascript是区分大小写的。你能解释一下这是什么意思吗。label-${val}这被替换为.label-1、.label-2。label-n取决于val是什么-这是此处单击的输入按钮的值
$("input[type='radio']").on("click", function() {
    var outputText = [];
    var current = this.nextSibling;
    while (current !== null && current.tagName !== "INPUT") {
        if (current.tagName) { //Check if element or node
            outputText.push(current.innerText)
        } else {
            outputText.push(current.nodeValue)
        }
        current = current.nextSibling;
    }
});