开发者

When using id's by jquery to do something, can the id's come from any kind of tag?

I am new to JQuery, and I am attempting writing code using PHP, HTML, and JQuery.

I want to replace all of the <?php echo $var; ?> in my HTML with tags that have id's, instead. For example, I want to have something like <div id="name">开发者_如何学Go;</div> which would then use a $("#name") to have it display something in that div field.

My questions: Does it have to be a <div> tag with that id when I call the #name in jquery? Or can it be in any tag and still have the same effect? --Would this work as well: <a href='alink'><div id="name"></div></a>. What if I want to put tags with id's to replace the alink--What tags would be good for that?

Any help appreciated.


You can add an id to any field. However, you can only use that particular id ONCE per page.

Ex.

<div id="name"></div>
<p id="name"></div>
<a id="name"></a>

The above is invalid and will cause problems if you use $('#name') in your jQuery. If you want to alter multiple things, like shown above, you are better off using a class and a different kind of selector:

<div class="name"></div>
<p class="name"></div>
<a class="name"></a>

$('.name')

This will select both of the elements.


An id can be an attribute of any html tag (element).

It also must be completely unique to the html document at any given time.

<?php echo $var; ?> however is not html at all, it is PHP which is something separate all together.


It can be any tag (=element), not just divs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜