每篇文章仅做为自己的备忘笔记,若有描述不清或不对的地方还请指明,感谢^_^

jq--动态添加 HTML 和 添加/移除指定的css

1、在指定元素下,添加 HTML 

  这里有两种方法,一种是使用“$("#元素").html()”;另一种是使用:“$("#元素").append”;

“$("#元素").html()”

htmlText1 += '<option value="' + thisData[i].id + '">' + thisData[i].name + '</option>';

$("#newSelect1").html(htmlText1);

//.html会覆盖指定元素下的内容,所以需要有变量进行接收

“$("#元素").append”:

 $("#newSelect2").append('<option value="' + thisData[i].id + '">' + thisData[i].name + '</option>')
 //.append会在指定元素下的内容的结尾处添加新的,不会修改原内容

 2、指定位置添加/移除 指定的css

通过button的点击事件,添加指定的css

$("button").click(function(){
    $("p").addClass("cssName");
  });

通过button的点击事件,移除指定的css

$("button").click(function(){
    $("p").removeClass("cssName");
  });

 

posted @ 2019-09-01 22:35  菜汤不甜  阅读(658)  评论(0编辑  收藏  举报