Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.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 获取具有相同类的段落的值_Javascript_Jquery - Fatal编程技术网

Javascript 获取具有相同类的段落的值

Javascript 获取具有相同类的段落的值,javascript,jquery,Javascript,Jquery,基本上,我在一个滑块中有大约20个段落,每个段落都有一个共享按钮,可以复制它的innerHTML 例如: <p class="parag">content</p> <button onclick="share();"></button> <p class="parag">diff content</p> <button onclick="share();"></button> <p class=

基本上,我在一个滑块中有大约20个段落,每个段落都有一个共享按钮,可以复制它的innerHTML

例如:

<p class="parag">content</p>
<button onclick="share();"></button>
<p class="parag">diff content</p>
<button onclick="share();"></button>
<p class="parag">another content</p>
<button onclick="share();"></button>
内容

不同内容

其他内容

为了一次得到一段文字,我可以制作类似数组的东西吗?因为当我按下第一个共享按钮时,代码会复制第一段,如果我按下第二个共享按钮,我仍然会复制第一段而不是第二段。 希望我能正确解释我的问题,并提前向您表示感谢。

试试:

$(文档).ready(函数(){
myArray=[];
$(“p”)。每个(函数(){
警报($(this.attr(“类”))
myArray.push($(this.attr(“类”))
})
})

内容

不同内容

其他内容

在函数中也要提到此

试试这个

$(“按钮”)。单击(函数(e){
警报($(this.prev().text());
});

内容

分享 不同内容

分享

其他内容

分享
你能做的是

HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p class="parag">content</p>
<button>1</button>
<p class="parag">diff content</p>
<button>2</button>
<p class="parag">another content</p>
试试这个

$(文档).ready(函数(){
$(“按钮”)。单击(函数(e){
警报($(this.prev(“.parag”).text());
});
})

内容

内容 不同内容

差异

其他内容

另一个
您可以这样尝试

内容

不同内容

另一个内容

共享功能

function share(that){ alert($(that).prev('.parag').html()); }

我认为应该包括
share()的函数是的,我在编写示例代码时输入了一个错误:pNope不起作用。我试图做的是获取class.value,然后在共享函数中添加test[1](test是我分配的变量)好的,我在javascript中添加了p类和数组访问
function share(button) {
    alert(button.prev().text());
}
$(document).ready(function () {
    $('button').click(function () {
        share($(this))
     });
});
function share(that){ alert($(that).prev('.parag').html()); }