jquery选择第一个字母? [英] Jquery select first letter?
问题描述
我只是试图让jquery识别段落的第一个字母。我怎么做到这一点?
例如,我在页面上有一个页面,其中包含许多段落。我只希望从指定的字母开始的段落被授予类当前,并且所有其他字段被隐藏。我几乎知道如何添加类并隐藏其他类,但是,我不能让jquery识别第一个字母。
其次,是否可以拉这是来自url字符串的'第一个字母'变量吗?
例如,
Page 1 - 有一个字母列表。用户点击'B'并且网址是
http://domain.com/page2.html?letter=b
然后page2提取变量(b)并将其应用于jquery,只显示那些段落
> jQuery('p')。each(function(){
if(jQuery(this).text()。substr(0,1).toUpperCase()=='B' ){
jQuery(this).addClass('someclass')
}
})
您可以使用PHP来清理变量并将其打印在JS中:
<脚本类型= 文本/ JavaScript的 >
var letter ='<?php echo(strlen($ _ GET ['letter'])== 1)? $ _GET ['letter']:''; ?>'
< / script>
或者直接用 document.location
并提取它。
I am simply attempting to get jquery to identify the first letter of a paragraph. How would I do this?
For example, I have a page with a number of paragrahs on a page. I would like only the paragraphs starting with a specified letter to be given the class "current" and all others to be hidden. I pretty much know how to add the class and hide the others but, I cant get jquery to recognize the first letter.
Secondly, is it possible to pull this 'first letter' variable from the url string?
For example, Page 1 - There is a list of letters. The user clicks 'B' and the url is
http://domain.com/page2.html?letter=b
And page2 picks up that variable (b) and applies it to the Jquery, showing only those paragraphs
Try:
jQuery('p').each(function(){
if(jQuery(this).text().substr(0,1).toUpperCase() == 'B'){
jQuery(this).addClass('someclass')
}
})
You can use PHP to clean the variable and the print it in JS:
<script type="text/javascript">
var letter = '<?php echo (strlen($_GET['letter']) == 1) ? $_GET['letter'] : ''; ?>'
</script>
Or just grab it with document.location
and extract it.
这篇关于jquery选择第一个字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!