2011-05-06 94 views
3

在HTML像什么是id = name = .something #something?

<input class="new" type="text" name="title" id="title2" /> 

和CSS属性才能看到

.something { ... } 
#something { ... } 

什么是id=name=.something#something使用?

+0

有趣的你应该知道jQuery和服务器端的web开发,但不知道这些属性和选择器。 – BoltClock 2011-05-06 10:35:11

回答

4
  • ID:为DOM元素唯一标识符
  • 名称:参考元件具有ID“某物”
  • :提交其被用作所述数据检索关键
  • #something表单时要使用的名称
  • .something:参照元素(S)与类名 '东西'

HTML和C的这些都是真的基本概念SS。您可能需要阅读basic HTML tutorial以了解有关此主题的更多信息,尤其是attributes section

ID和类名主要用于CSS样式元素,并使用JavaScript向其添加行为。 例如:

HTML:

<button id="foo">Click me to unleash the Unicorn</button> 

CSS:

#foo { 
    border: 1px solid #ff0000; 
    font-weight: bold; 
    background: #000; 
    color: #fff; 
} 

的JavaScript:

document.getElementById('foo').onclick = function() { 
    var img = document.createElement('img'); 
    img.src = 'http://display.ubercomments.com/6/23672.jpg'; 
    document.getElementsByTagName('body')[0].appendChild(img); 
}; 

See also, this beautiful example (Unicorn included).

2

id属性是DOM中元素的唯一标识符。它的独特之处在于,您不能在文档中包含具有此ID的多个元素。

基于ID对元素进行造型使用#something完成。

A name属性只是该元素的非唯一名称。这在表单中最常用,名称是POST'd或GET'直到服务器端语言。

.something是任何元素上class=属性的样式选择器。在任何的以下三种方式中<div class="testclass" name="testname" id="testid"></div>

举例来说,你可以样式以下元素

.testclass { 
    background-color: black; 
} 

#testid { 
    background-color: black; 
} 

div[name="testname"] { 
    background-color: black; 
} 

请记住,这两个类和名称都唯一的,因此它们可用于样式和定义多个元素。

0
  • id="something"表示ID。你只能拥有一次。它的CSS参考是#something。另外,通过在地址末尾使用#something,您可以直接将浏览器移动到该ID,而无需使用JS。
  • name=发送表单时使用。使用PHP,您可以使用$_REQUEST['title']来检查该值。在其他编程语言中,也有获取该值的方法。
  • .something是CSS中的类。它被用来风格HTML元素与class="something"
1

.something是一类,而#something是一个id。 Name =属性通常用于表单,通常不用于CSS。 换句话说,下面的代码:

<body class="thisisaclass"> 
<div id='thisisanid'></div> 
<div class='thisisanotherclass'></div> 
</body> 

将导致CSS,看起来像这样:如果你想使用相同类型的

.thisisaclass {..Code..} 
.thisisaclass #thisisanid {..Code..} 
.thisisanotherclass {...code...} 

类用于重复的东西,例如在页面的几个区域中设置文本格式 - 而id只能在html代码中出现一次。

退房HTMLDog了解更多,这是一个很好的开始:)

+0

http://w3fools.com/:P – xfix 2011-05-06 10:25:35

+0

@GlitchMr虽然教程不是先进的,就像我说的它是一个开始的好地方。很显然,任何优秀的程序员都应该结合并阅读不止一个来源来学习。我不知道w3schools是**,尽管> _>糟糕 – Exodus 2011-05-06 10:46:45

0

类是例如多重选择,如果你想多个表具有相同的颜色,回地面颜色和字体等,您将定义类。 在这些表格中,如果某个特定表格以不同的方式显示样式,您将使用id。 Id不能被复制。而且你可以为同样多的对象分配你想要的对象。

<style type="text/css"> 
.MyTable{ 
background-color:#ff00ff; 
} 
#centralTable{ 
background:color:red; 
} 
</style> 

<div class="MyTable">Data </div> 
<div class="MyTable"> </div> 
<div class="MyTable" id ="centralTable"> Data</div> 
<div class="MyTable"> Data</div> 
<div class="MyTable">Data </div> 

记住类随后在层叠样式表期间(.)和IDS(#)。

相关问题