Sítios Web II [SW2]

$('manupulation')

ProfºEduardo Pezutti

http://maverick.td.utfpr.edu.br/slides/sw2 link

jQuery

Manipulando o DOM

jQuery

Pegando conteúdo do HTML (DOM)

  • text()
  • html()
  • attr()
  • val()

text()

Pega o texto (conteúdo) de uma marcação


$("#btn1").click(function(){
  alert("Text: " + $("#test").text());
});
Teste

html()

Pega o texto (conteúdo) e as marcações html de uma marcação atingida


$("#btn2").click(function(){
  alert("HTML: " + $("#test").html());
});
Teste

val()

Retorna o valor de um input, ou textarea


$("#btn1").click(function(){
  alert("Value: " + $("#test").val());
});
Teste

attr()

Retorna o valor de um atributo


$("button").click(function(){
  alert($("#w3s").attr("href"));
});
Teste

jQuery

Colocando conteúdo no HTML (DOM)

  • text()
  • html()
  • attr()
  • val()

text(), html() e val()


$("#btn1").click(function(){
  $("#test1").text("Hello world!");
});

$("#btn2").click(function(){
  $("#test2").html("<b>Hello world!</b>");
});

$("#btn3").click(function(){
  $("#test3").val("Dolly Duck");
});
Teste

text(), html() e val()

Callback


$("#btn1").click(function(){
  $("#test1").text(function(i,origText){
    return "Old text: " + origText + " New text: Hello world!
  (index: " + i + ")"; 
  });
});
Teste

attr()


$("button").click(function(){
  $("#w3s").attr("href","http://www.w3schools.com/jquery");
});
Um ou Múltiplos

attr()

Callback


$("button").click(function(){
  $("#w3s").attr("href", function(i,origValue){
    return origValue + "/jquery"; 
  });
});
Teste

jQuery

Adicionando marcações novas

  • append()
  • prepend()
  • after()
  • before()

append()

Coloca uma marcação no fim da seleção


$("p").append("Some appended text.");
Teste

prepend()

Coloca uma marcação no início da seleção


$("p").prepend("Some appended text.");
Teste

Múltiplos elementos

Com append() e prepend()

Exemplo

after() e before()

Coloca o conteúdo DEPOIS ou ANTES da marcação selecionada


$("img").after("Some text after");

$("img").before("Some text before");
Teste

Múltiplos elementos

Com after() e before()

Exemplo

jQuery

Removendo marcações

  • remove()
  • empty()

remove()

Remove a seleção e seus filhos


$("#div1").remove();
Teste e Teste (com filtro)

empty()

Remove apenas os filhos


$("#div1").empty();
Teste

jQuery

Manipulando CSS

  • addClass()
  • removeClass()
  • toggleClass()
  • css()

addClass()

Adiciona uma class na seleção


$("button").click(function(){
$("h1,h2,p").addClass("blue");
$("div").addClass("important");
});
Teste 1 e Teste 2

removeClass()

Remove uma class da seleção


$("button").click(function(){
$("h1,h2,p").removeClass("blue");
});
Teste

toggleClass()

Adiciona / Remove uma class da seleção


$("button").click(function(){
$("h1,h2,p").toggleClass("blue");
});
Teste

css()

Coloca CSS na seleção


$("p").css("background-color","yellow");

$("p").css({"background-color":"yellow","font-size":"200%"});
Teste 1 e Teste 2

ProfºEduardo Pezutti

epsantos@utfpr.edu.br link