Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.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中的按钮时,如何为一个div添加css样式_Javascript_Html_Css_Angularjs - Fatal编程技术网

Javascript 当我单击另一个div中的按钮时,如何为一个div添加css样式

Javascript 当我单击另一个div中的按钮时,如何为一个div添加css样式,javascript,html,css,angularjs,Javascript,Html,Css,Angularjs,当我在另一个div中单击一个按钮时,我正在尝试为一个div文本添加一些css样式 这是我的html代码 <div class="row" id="target"> <h2>{{txt}} </h2> </div> <div class="row"> <button my-dir>Click Me</button> </div> .directive('myDir', function (

当我在另一个div中单击一个按钮时,我正在尝试为一个div文本添加一些css样式

这是我的html代码

<div class="row" id="target">
  <h2>{{txt}} </h2>
</div>


<div class="row">
  <button my-dir>Click Me</button>
</div>
.directive('myDir', function () {
  return {
    restrict: 'A',

    link: function (scope, elem, attr) {
      console.log(elem); 
      elem.bind('click', function (e) {
        console.log("coming");
        elem.find('#target').css({
          backgroundColor: 'red'
        })    

        // angular.element(e.target).siblings('#target').addClass('clr');
      });

    }
  }
});
我的问题是,当我点击一个div中的按钮时,css样式必须应用于另一个divusing指令,我尝试了上面的方法,但没有成功


提前感谢

elem
将参考
按钮
。因此
elem.find(“#target”)
将在
elem中查找
#target

你可以这样做

elem.bind('click', function (e) {
        $('body').find('#target').css({
          backgroundColor: 'red'
        })    
});

elem
将参考
按钮
。因此
elem.find(“#target”)
将在
elem中查找
#target

你可以这样做

elem.bind('click', function (e) {
        $('body').find('#target').css({
          backgroundColor: 'red'
        })    
});

如果您的元素id是唯一且未更改的,那么您可以将其简化如下

 elem.bind('click', function () {
         $('#target').css('backgroundColor','red');
 });

elem.find将查找id包含“target”的所有div,尝试对所有div更改使用唯一id。它将对您有所帮助。

如果您的元素id是唯一的且未更改,那么您可以将其简化为以下内容

 elem.bind('click', function () {
         $('#target').css('backgroundColor','red');
 });
elem.find将查找id包含“target”的所有div,尝试对所有div更改使用唯一id。它会帮助你的