Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.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
在jquery中添加一段时间不起作用的css类_Jquery_Css - Fatal编程技术网

在jquery中添加一段时间不起作用的css类

在jquery中添加一段时间不起作用的css类,jquery,css,Jquery,Css,我从颜色选择器中检测到一个类,希望更改标题标记的类 // to find specific header id which having bgt css class . Detecting id from a div which have css class .ss $(document.body).on('click', '.bgt', function(e) { e.preventDefault(); id = $(this).closest('div').find('.s

我从颜色选择器中检测到一个类,希望更改标题标记的类

// to find specific header id which having bgt css class . Detecting id from a div which have css class .ss

$(document.body).on('click', '.bgt', function(e) {
    e.preventDefault();
    id = $(this).closest('div').find('.ss').attr('id');
    var $this = $('#h'+id); 
    var $dv = $this.closest('div');
    var nstr= $dv.attr('class');
    var newstrt = nstr;
    //heder tag where I am to add class
    $("#h"+id).addClass(newstrt);
这有时工作有时不工作
请提供帮助

您应该在要添加的类中引用:

$("#h"+id).addClass("newstrt");

请更改
e.preeventDefault()


我认为您可以使用
var id
来代替
id

$(document.body).on('click', '.bgt', function(e) {
e.preventDefault();
var id = $(this).closest('div').find('.ss').attr('id');

//var $this = $('#h'+id); 
//var $dv = $this.closest('div');
//var nstr= $dv.attr('class');
//var nstrr = nstr;

//heder tag where I am to add class
$("#h"+id).addClass(newstrt);

如果你看你的代码。我认为不需要四行注释。

你应该打开你的js控制台,每次它不起作用时都查看错误消息。在我看来,这非常混乱。你想要实现什么?首先查找div的ID,然后执行
$(“#”+ID)
。然后又是那些乱七八糟的东西…你需要分享一个html示例。。。另外,检查浏览器控制台是否存在任何错误检查循环中是否存在id.Ashish Kumar头标记的重复,并且每个头标记具有不同的唯一id。假设
newstart
是所需的类名,而不是在别处声明的变量,则这是正确的。newstrt是一个具有类名的变量,因此在qoute中是正确的写相同的名字newsrtr。实际上,newstrt有一个名为bg color green的类。这些是不断变化的,你在哪里看到了bg颜色绿色?bg颜色绿色我从下拉列表中检测到这个类,并希望在标题处应用。@Attarisoft,其中你声明了newstrt这四行正在检测我要应用的类名。@Pramod什么是newstrthere@Attarisoft你还没有在这四行中声明newstrt。所以我评论说四个lines@prash如果你看那四行,newstrt在哪里?我以为他在别的地方申报了变量
$(document.body).on('click', '.bgt', function(e) {
e.preventDefault();
var id = $(this).closest('div').find('.ss').attr('id');

//var $this = $('#h'+id); 
//var $dv = $this.closest('div');
//var nstr= $dv.attr('class');
//var nstrr = nstr;

//heder tag where I am to add class
$("#h"+id).addClass(newstrt);