how to use one class with many elements? - html

for some reasons i have somthing like this in my html code
id = 1
<li class="anyclass" ></li>
<li class="firstclasname + id" ></li>
<li class="scondclasname + id" ></li>
<li class="thirdclasname + id ></li>
<li class="forthclasname + id" ></li>
<li class="fifthclasname + id" ></li>
</ul>
id = 2
<li class="anyclass" ></li>
<li class="firstclasname + id" ></li>
<li class="scondclasname + id" ></li>
<li class="thirdclasname + id ></li>
<li class="forthclasname + id" ></li>
<li class="fifthclasname + id" ></li>
</ul>
can i make just one css file for this code contain something like this :
.every first calss name + what ever id {
background-image: url('/image/facebook.png');
background-repeat: no-repeat;
background-size: auto;
background-position: -277px -446px;
}

You just need to try nth-child of CSS3 property.
ul.yourClass li.firstclasname:nth-child(2){
background:url('/image/facebook.png') -277px -446px no-repeat;
background-size:auto;
}

You can use starts with [attr^=value] attribute selector.
li[class^="firstclasname"] {
background: blue;
}
<ul>
<li class="anyclass"></li>
<li class="firstclasname1"></li>
<li class="scondclasname1"></li>
<li class="thirdclasname1"></li>
<li class="forthclasname1"></li>
<li class="fifthclasname1"></li>
</ul>
<ul>
<li class="anyclass"></li>
<li class="firstclasname2"></li>
<li class="scondclasname2"></li>
<li class="thirdclasname2"></li>
<li class="forthclasname2"></li>
<li class="fifthclasname2"></li>
</ul>

For cases like this I like to use two selectors -- a common one, relevant to all the items, and a specific one to identify a single item.
An example use would be something like this:
<ul class="contact-list">
<li class="item item-1">Contact 1</li>
<li class="item item-2">Contact 2</li>
<li class="item item-3">Contact 3</li>
</ul>
In CSS you can then target the list itself, all the items or one specific item.
Edit
Apologies, I skimmed your question and missed the part about id=1 and id=2. There are a few different options depending on how many IDs there will be and whether or not this will be known.
For a short list of known ID's you could just target the individual items as I mentioned. If there are an unknown number of IDs (or the IDs themselves are unknown) you may consider using the nth-child selector.
This depends entirely on the effect you're trying to achieve, which isn't detailed in your question, but some commonly used (in the context of my example markup above) are as follows:
.contact-list:nth-child(odd) applies to every odd element, i.e. 1st, 3rd, 5th
.contact-list:nth-child(even) applies to every even element, i.e. 2nd, 4th, 6th
.contact-list:nth-child(1) applies only to the first element
.contact-list:nth-child(4n+4) applies to every fourth element
And so on. Most common requirements have been done a thousand times before and can be found with a quick google search along the lines of "CSS every second element".

Related

Why is my attribute selector working when it doesn't have any whitespace in it?

MDN states the following about the [attr~=value] attribute selector:
Represents elements with an attribute name of attr whose value is a
whitespace-separated list of words, one of which is exactly value.
If liquid is not separated by whitespace, then why is it working?
[data-vegetable~="liquid"] {
color: red;
}
Ingredients for my recipe: <i lang="fr-FR">Poulet basquaise</i>
<ul>
<li data-quantity="1kg" data-vegetable>Tomatoes</li>
<li data-quantity="3" data-vegetable>Onions</li>
<li data-quantity="3" data-vegetable>Garlic</li>
<li data-quantity="700g" data-vegetable="not spicy like chili">Red pepper</li>
<li data-quantity="2kg" data-meat>Chicken</li>
<li data-quantity="optional 150g" data-meat>Bacon bits</li>
<li data-quantity="optional 10ml" data-vegetable="liquid">Olive oil</li>
<li data-quantity="25cl" data-vegetable="liquid">White wine</li>
</ul>
It's not separated by whitespace but:
a whitespace-separated list of words
Which mean a list of words where you have whitespace between and if alone no need whitespace because there is nothing to separate.
[data-vegetable~="liquid"] {
color: red;
}
<ul>
<li data-vegetable="liquid other and other">this one</li>
<li data-vegetable="liquid">and this one</li>
<li data-vegetable="liquid ">also this one</li>
<li data-vegetable="another liquid ">also this one</li>
<li data-vegetable="liquid-one">NOT this one !!</li>
<li data-vegetable="another-liquid">NOT this one !!</li>
<li data-vegetable="aliquid">NOT this one !!</li>
</ul>
For the last ones you will need *
[data-vegetable*="liquid"] {
color: red;
}
<ul>
<li data-vegetable="liquid other and other">this one</li>
<li data-vegetable="liquid">and this one</li>
<li data-vegetable="liquid ">also this one</li>
<li data-vegetable="another liquid ">also this one</li>
<li data-vegetable="liquid-one">this one too!!</li>
<li data-vegetable="another-liquid">this one too!!</li>
<li data-vegetable="aliquid">this one too!!</li>
</ul>
A list of words has separators between the words.
If the list contains only one word, there are no separators
What that means is, the attribute (data-vegetable in this case) can have multiple values, assigned to it in one string using whitespace to delimit the values, like data-vegetable="liquid foo bar", which gives data-vegetable three values 'liquid', 'foo' and 'bar'.
The selector can be used to match any of these values, so you will get this element when matching either [data-vegetable~="liquid"], [data-vegetable~="foo"] or [data-vegetable~="bar"].
It said: "whitespace-separated list of words"
That means that it is a list, separated by whitespace. But a list can have only one item. So in that case, no spaces are needed.

Avoid writing multiple variable values in Angular 2 when changing class of clicked items

I hope this is a good question and I am not just missing something total simple. I am very new to Angular 2 and I am always into saving code lines and time :)
I want to change the active css class of my tabs (I dont want to use router!) and I ended up with something like this:
activeTab: string;
switchActiveTab(newTab: string) {
this.activeTab = newTab;
}
<div class="buttonLine">
<ul class="nav nav-pills">
<li role="presentation" [ngClass]="{'active': activeTab === 'Example Tab 1'}" (click)="switchActiveTab('Example Tab 1');">
<a>Example Tab 1</a>
</li>
<li role="presentation" [ngClass]="{'active': activeTab === 'Example Tab 2'}" (click)="switchActiveTab('Example Tab 2');">
<a>Example Tab 2</a>
</li>
</ul>
</div>
So I had to declare the string value "Example Tab 1" three times in my HTML. This is pretty annoying, especially when I would have 5 or more tabs here.
Is it possible to avoid reapeating the expression "Example Tab 1" three times in my HTML? Or is it possible to do this kind of stuff in a more elegant way?
Method 1
To simplify the template code, you can declare the list of tabs in the component class:
public tabList: Array<string> = ["Example Tab 1", "Example Tab 2"];
and generate the li elements with the *ngFor directive:
<li *ngFor="let tab of tabList" [ngClass]="{'active': activeTab === tab}" (click)="switchActiveTab(tab);" role="presentation">
<a>{{tab}}</a>
</li>
Method 2
To keep the code more declarative, each item could refer to itself with a template reference variable instead of using the tab caption (as illustrated in this plunker):
<div class="buttonLine">
<ul class="nav nav-pills">
<li #tab1 [ngClass]="{'active': activeTab === tab1}" (click)="switchActiveTab(tab1);" role="presentation">
<a>Example Tab 1</a>
</li>
<li #tab2 [ngClass]="{'active': activeTab === tab2}" (click)="switchActiveTab(tab2);" role="presentation">
<a>Example Tab 2</a>
</li>
</ul>
</div>
The code would be modified accordingly:
activeTab: HTMLElement;
switchActiveTab(newTab: HTMLElement) {
this.activeTab = newTab;
}

Ng-click not working dragula drag-drop elements

Using dragula plugin (Angular 1) link
ng-click not working moved (drag and drop to another ul) on li element
<ul dragula='"second-bag"'>
<li ng-click="fun()">Item One </li>
<li ng-click="fun()">Item Two</li>
<li ng-click="fun()">Item Three</li>
<li ng-click="fun()">Item Four</li>
</ul>
<ul dragula='"second-bag"'>
<li ng-click="fun()">Item One </li>
<li ng-click="fun()">Item Two</li>
<li ng-click="fun()">Item Three</li>
<li ng-click="fun()">Item Four</li>
</ul>
app.controller('ExampleCtrl', ['$scope', function ($scope) {
$scope.fun = function(){
alert('test');
}
}]);
It is probably the expected behaviour of dragula, becouse in order to drag the element you are actually clicking it.
The important part is why do you want to listen the clicking event of an dragula list element? If the answer is to manipulate that particular element or do another operation, dragula gives you a set of opportunities.
<div dragula='"sixth-bag"'></div>
<div dragula='"sixth-bag"'></div>
app.controller('MuchExampleCtrl', ['$scope', 'dragulaService',
function ($scope, dragulaService) {
dragulaService.options($scope, 'sixth-bag', {
moves: function (el, container, handle) {
return handle.className === 'handle';
}
});
}
]);
In this example, you are changing the className of the "handled" element. Similar to this, you can use this approach for other possible outcomes.
Links:
http://bevacqua.github.io/angular-dragula/
https://github.com/bevacqua/dragula#optionsmoves
Also as an alternative you might want to checkout the service ngDraggable for more "Angular1 style" syntax.
ngDraggable: https://github.com/fatlinesofcode/ngDraggable

Click a link in IE through VBA

I am trying to click a link but it is different from others links I have made it. I don't have a field called "id" or something.
Here is the HTML. I need it to click "pibmunic super" button/link. The FIREPATH XPath shows .//*[#id='informacoes_estatisticas']/ul/li[20]/span
<div id="informacoes_estatisticas">
<h3 class="titulo">Informações Estatísticas</h3>
<ul class="links">
<li class="item Censo Agropecuário_2006">
<li class="censo2010 super">
<li class="educa super">
<li class="empresas super">
<li class="vida super">
<li class="item Estimativa da População 2014_">
<li class="prodext2013 super">
<li class="financas super">
<li class="frota super">
<li class="item Fundações Privadas e Associações sem Fins Lucrativos no Brasil 2010_">
<li class="item Índice de Desenvolvimento Humano Municipal - IDHM_">
<li class="instfin super">
<li class="item Mapa de Pobreza e Desigualdade - Municípios Brasileiros_2003">
<li class="morbid super">
<li class="prodpec2013 super">
<li class="item Pesquisa Nacional de Saneamento Básico_2008">
<li class="item Produção Agrícola Municipal - Cereais, Leguminosas e Oleaginosas_2007">
<li class="lavperm2013 super">
<li class="lavtemp2013 super">
<li class="pibmunic super">
<span class="super">Produto Interno Bruto dos Municípios</span>
<ul class="pibmunic sub">
</li>
<li class="partpol super">
<li class="assismed super">
<li class="snig_censo2010 super">
</ul>
</div>
I am trying to write my code as something like this
Set m = html.getElementById("something")
Set a = m.getElementsByTagName("something")(something)
a.Click
I am not sure this is right, I have tried lots of combinations and it does not work. I don't know exactly which "getelementsby" I should use or what to give the command inside ().
Hope you can help me !! thanks
All of your classes seem to be unique. If that's the case you should change all the class attributes to id attributes ex:
class="something"
to
id="something"
Once you do that getElementById("something") will work. getElementsByTagName() returns an array of elements with a specific tag. ex:
getElementsByTagName("li")
retrieves all the li elements under a node.
If the newest VBA supports querySelectorAll() that will allow you to use CSS selectors to find elements. ex:
querySelectorAll(".something")
This will find all elements with the class something. However, since there can be multiple elements with the same class, you will get an array as a result. You can either loop over these elements to process them, narrow your selector, or get the first index of the returned array.
If VBA still does not support querySeletorAll() and you will need to combine getElementsByTagName() with a loop over the matching tags to find elements with the something class. ex:
For Each el in getElementById("container").getElementsByTagName("li")
For Each cl in el.className.Split(' ')
If cl = "something" Then
el.onclick()
End If
Next
Next
I would recommend assigning Id's or if at all possible use JavaScript or jQuery. They are more specialized at handling at these tasks (more abstraction). jQuery would be as simple as:
$(".something").trigger("click");

Mootools - getFirst().get('text') not a function?

the HTML:
<ul id="nav">
<li id="listItem">a list item</li>
<li id="link01">list item with ID</li>
<li id="link02">another link with ID</li>
<li class="lastItem">Contact</li>
<li class="lastItem">the Very Last List Item</li>
</ul>
the JavaScript:
alert($$('.lastItem').getFirst('li').get('text'));
console returns this error:
TypeError: $$(...).getFirst(...).get is not a function
um...whut? what did i miss? if i take out the getFirst(), it works, but returns, of course, both <li> text contents... which i don't want. just want the first...
halp.
WR!
You trying to call getFirst on Elements array($$ return elements array!) the getFirst() method is only on a dom mootools element and it will return his first child
what you are looking for is this:
alert($$('.lastItem')[0].get('text'));