[英]how can i add css style for a div when i clicked on a button in another div
I'am trying to add some css styles for a div text, when I clicked a button in another div 当我单击另一个div中的按钮时,我正在尝试为div文本添加一些CSS样式
here is my html code 这是我的HTML代码
<div class="row" id="target">
<h2>{{txt}} </h2>
</div>
<div class="row">
<button my-dir>Click Me</button>
</div>
my js code 我的js代码
.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');
});
}
}
});
my problem is when I clicked on button in one div css styles has to apply for the another div using directive, I tried something like above but didn't worked. 我的问题是,当我单击一个div css样式中的按钮必须使用指令申请另一个div时,我尝试了上述类似操作,但没有成功。
Thanks in advance 提前致谢
elem
will refer to the button
. elem
将引用该button
。 So elem.find('#target')
will look for #target
inside elem
因此
elem.find('#target')
将在elem
寻找#target
You can do like this 你可以这样
elem.bind('click', function (e) {
$('body').find('#target').css({
backgroundColor: 'red'
})
});
if your element id is unique and non changed then you can simplify it as below 如果您的元素ID是唯一且未更改,则可以如下进行简化
elem.bind('click', function () {
$('#target').css('backgroundColor','red');
});
elem.find will find all div with id contains 'target', try to use unique id's for all div changes. elem.find会找到ID包含“目标”的所有div,并尝试对所有div更改使用唯一的ID。 It will help you.
它会帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.