简体   繁体   English

通过类名称和属性值获取元素并在AngularJS中设置CSS

[英]Get element by class name and attribute value and set CSS in AngularJS

I have a directive: 我有一条指令:

class LkfsSliderDirective {

constructor() {
    this.restrict = "E";
    this.templateUrl = "./controls/lkfsSlider/lkfsSliderTemplate.html";
    this.controller = 'LkfsSliderController';
    this.controllerAs = 'slider';
    this.replace = true;
    }

    link(scope, element, attrs) {

    }
}

export default LkfsSliderDirective;

In HTML: 在HTML中:

<img ng-src="{{slide.image}}" class="lkfs-slider-slide" alt="" id="{{slide.id}}" ng-repeat="slide in slider.slides"/>

I would like get img tag by class name with specific id (for example = 0) and set CSS ( display: block ). 我想通过具有特定ID(例如= 0)的类名称获取img标签,并设置CSS( display: block )。

I tried in many ways but nothing works, for example: 我尝试了很多方法,但没有任何效果,例如:

var elem = document.getElementsByTagName("img")[0];
angular.element(elem).css('display', 'block');

Also in link. 也在链接中。

Any ideas? 有任何想法吗?

Just select the element with the specific ID using document.getElementById() : 只需使用document.getElementById()选择具有特定ID的元素:

var elem = document.getElementById("0");
angular.element(elem).css('display', 'block');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在AngularJs中获取和设置元素的类 - Get and Set class of an element in AngularJs 在angularjs中获取div元素的高度并将其设置为另一个元素的属性 - Get height of div element in angularjs and set it to another element's attribute 如何在jquery或angularjs中获取具有特定属性的标签名称的元素 - How to get element with tag name having specific attribute in jquery or angularjs 如何根据 Svelte 中同一元素的另一个属性设置 class 名称? - How to set a class name dependant on another attribute of the same element in Svelte? 是否可以通过属性的值获取元素ID并将属性设置为parentNode - Is it possible to get element id by attribute's value and set an attribute to the parentNode 如何通过名称获取元素并设置其值 - How to get an element by name and set its value 根据 ID 和特定属性值向元素添加 CSS 类 - Add CSS class to element based on the ID and a specific attribute value 根据特定属性的值获取元素的类或ID名称 - Getting class or ID name of an element based on the value of a particular attribute 通过使用html自定义属性值将类名称添加到html元素 - Add class name to a html element by using html custom attribute value 如何将angularjs变量值赋给html元素属性,例如输入elememnt的name属性 - How to assign angularjs variable value to html element attribute, such as input elememnt's name attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM