Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.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 如何在警报框中获取div内容或将其存储为变量?_Javascript_Jquery - Fatal编程技术网

Javascript 如何在警报框中获取div内容或将其存储为变量?

Javascript 如何在警报框中获取div内容或将其存储为变量?,javascript,jquery,Javascript,Jquery,我在一个div中有3个内容,一次只显示一个内容。我附加了一个prev和next按钮,使用jquery一次显示一个div的内容。 我现在想做的是在单击任意一个按钮时,在一个警报框中获取div的内容警报(“div中的内容”)或至少将其存储在要传递给控制器的变量中 下面是我的代码: HTML: 废话 废话 废话废话 JQUERY: $('#tabs div').first().attr('class', 'current'); $('#tabs div').each(function(i) {

我在一个div中有3个内容,一次只显示一个内容。我附加了一个prev和next按钮,使用jquery一次显示一个div的内容。
我现在想做的是在单击任意一个按钮时,在一个警报框中获取div的内容<代码>警报(“div中的内容”)或至少将其存储在要传递给控制器的变量中

下面是我的代码:

HTML:


废话
废话
废话废话
JQUERY:

$('#tabs div').first().attr('class', 'current');

$('#tabs div').each(function(i) {
i = i + 1;

$(this).attr('id', 'tab-' + i);

if(i !== $('#tabs div').size()) {
    $(this).append('<button class="tabPagination" rel="tab-' + (i + 1) + '">Next</button>');
}
if(i !== 1) {
    $(this).append('<button class="tabPagination" rel="tab-' + (i - 1) + '">Previous</button>');
}                
});            

    $('#tabs div[class!="current"]').hide();

    $('.wkend').on('click', '.tabPagination', function(evt) {
   evt.preventDefault();
       alert('content in the div');
       $('.current').removeAttr('class');
       $('#tabs div[class!="current"]').hide();
       $('#' + $(this).attr('rel')).show();
});
$('#tabs div').first().attr('class','current');
$('#tabs div')。每个(函数(i){
i=i+1;
$(this.attr('id','tab-'+i);
如果(i!=$('#tabs div').size()){
$(this.append('Next');
}
如果(i!==1){
$(this.append('Previous');
}                
});            
$('#tabs div[class!=“current”]')。hide();
$('.wkend')。on('click','.tabPagination',函数(evt){
evt.preventDefault();
警报(“div中的内容”);
$('.current').removeAttr('class');
$('#tabs div[class!=“current”]')。hide();
$('#'+$(this.attr('rel')).show();
});
html是我的视图,我希望在单击“下一步”和“上一步”按钮时获取div的内容,例如“诸如此类”

谢谢你的帮助。

var电流=0;
var max=3;
$(“#下一步”)。单击(函数(){
电流++;
如果(当前>最大值)
电流=最大值;
其他的
window.alert($('#tabs div:n子项('+current+')).text());
});
$('#prev')。单击(函数(){
当前--;
如果(电流<1)
电流=1;
其他的
window.alert($('#tabs div:n子项('+current+')).text());
});

警报($('#something').html())?或者我遗漏了什么?使用您当前的代码:
alert($('#'+$(this.attr('rel')).html())谢谢你们,你们都非常乐于助人。echochamber给了我我想要的东西。@Koala_dev and SuperScript,是否有可能只获得内容,例如Blah。。没有html和上一个、下一个文本。我使用了$('#'+$(this.attr('rel')).text();但还是有很多废话。我只想听到那些废话。非常感谢你。这正是我所需要的。嗨,我刚刚意识到所有的div内容都显示出来了,而我的代码却隐藏了它们。@Rebe24你能更详细地解释一下你想要实现什么吗?
$('#tabs div').first().attr('class', 'current');

$('#tabs div').each(function(i) {
i = i + 1;

$(this).attr('id', 'tab-' + i);

if(i !== $('#tabs div').size()) {
    $(this).append('<button class="tabPagination" rel="tab-' + (i + 1) + '">Next</button>');
}
if(i !== 1) {
    $(this).append('<button class="tabPagination" rel="tab-' + (i - 1) + '">Previous</button>');
}                
});            

    $('#tabs div[class!="current"]').hide();

    $('.wkend').on('click', '.tabPagination', function(evt) {
   evt.preventDefault();
       alert('content in the div');
       $('.current').removeAttr('class');
       $('#tabs div[class!="current"]').hide();
       $('#' + $(this).attr('rel')).show();
});
var current = 0;
var max = 3;
$('#next').click(function(){
     current++;
     if(current > max)
         current = max;
     else
         window.alert( $('#tabs div:nth-child('+current+')').text() );
});

$('#prev').click(function(){
     current--;
     if(current < 1)
         current = 1;
     else
          window.alert( $('#tabs div:nth-child('+current+')').text() );
});