1

wrap()私はjqueryが初めてで、コードに続くウェブサイトのメニューで使用する方法を見つけようとしています

HTML

<ul>
   <li><a href="#">Who We Are</a></li>
   <li><a href="#">Our Work</a></li>
</ul>

脚本

$('.menu li a').text().wrap('<span></span>');

しかし、うまくいきません:(

次のコードのようにコードを表示したいタグにタグHTMLを追加したいだけです<span><a>

コード

<ul>
   <li><a href="#"><span>Who We Are</span></a></li>
   <li><a href="#"><span>Our Work</span></a></li>
</ul>

事前に友達に感謝してください... :)

4

3 に答える 3

4
于 2012-12-09T09:54:06.980 に答える
3

まず、 selectorから返された要素を使用して呼び出すことnottest()できるメソッドがあり、次に、代わりにwrapInner()が必要です。wrap

ライブデモ

変化する

$('.menu li a').test().wrap('<span></span>');

$('ul li a').wrapInner('<span></span>');
于 2012-12-09T09:52:15.860 に答える
2

Wrap would put the span around the a.

You can do this :

$('.menu li a').each(function(){
     $(this).html('<span>'+$(this).html()+'</span>');
});

Demonstration (open the console to see the final HTML)

I suppose you meant text and not test. If so be aware that text doesn't return the text node but just a string, so you can't use it as a jQuery element and you can't call wrap on it.

于 2012-12-09T09:55:36.323 に答える