jQuery/attr()

解説
指定した属性の値を取得

.attr(property)

指定した属性の値を設定

.attr(property, value)

.attr(object)

.attr(function(index, property))

sample1
画像
sample1code
$(function(){
  $(".sample1").click(function(){
	$("#img").attr("src", $(this).attr("href"));
	return false;
  })
});
sample2
sample2Div
sample2code
$(function(){
  $("#sample2Div").on({
  	mouseover: function(){
  	  $("#sample2Img").attr({
  		'src': '../../img/reims.jpg',
  		'title': '@rise'
  	  });
  	},
  	mouseout: function(){
	  $("#sample2Img").removeAttr("src title");
	}
  });
});
	
sample3 YAHOO
sample3code
$(function(){
  $("#s3Btn").click(function(){
	$("#s3a").attr("href", "http://www.google.co.jp").text("GOOGLE");
  });
});
	
sample4
sample4code
$(function(){
  $("#s4Div img").css({
  	"width": "200px",
  	"height": "150px"
  });
  $("#s4Sub img").css({
  	"width": "100px",
  	"height": "75px"
  });
  $("#s4Sub img").hover(function(){
  	var src = $(this).attr("src");
  	$("#s4Div img").attr("src", src);
  });
});
	
sample5
  • aaa
  • bbb
  • ccc
  • ALL
th1 th2 th3
aaa aaa aaa
aaa aaa aaa
bbb bbb bbb
ccc ccc ccc
sample5code
$("#s5Ul li").click(function(){
  var s5Class = $(this).attr("class");
  $("#s5Table tr").css("display","none");
  $("#s5Table tr." + s5Class).show(); 
});
$("#all").click(function(){
  $("#s5Table tr").show();
});
	
sample6
s6Div
sample6code
$("div").click(function(){
  var thisId = $(this).attr("id");
  var thisClass = $(this).attr("class");
  var thisName = $(this).attr("name");
  console.log(thisId + "," + thisClass + "," + thisName);
});
	
sample7

reims jura code
sample7code
function s7Func(){
  var s7Alt = $(this).attr("alt");
  $("#s7Div p").text(s7Alt);
}
$("#s7Div img").on("click", s7Func);
	
sample8
sample8code
function s8Func(){
  var s8Obj = new Object();
  s8Obj.title = "sample8";
  s8Obj.id = "s8Id";
  s8Obj.class = "s8Class";
  s8Obj.type = "radio";
  s8Obj.value = "sample8";
  s8Obj.checked = true;
  $("#s8Btn").on("click", function(){
    $("#s8Input").attr(s8Obj).after(s8Obj.value);
  });
}