Javascript jquery选择第一个字母?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3039397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery select first letter?
提问by Batfan
I am simply attempting to get jquery to identify the first letter of a paragraph. How would I do this?
我只是试图让 jquery 识别段落的第一个字母。我该怎么做?
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.
例如,我有一个页面,页面上有多个段落。我只希望以指定字母开头的段落被赋予“当前”类,而所有其他段落都被隐藏。我几乎知道如何添加类并隐藏其他类,但是,我无法让 jquery 识别第一个字母。
Secondly, is it possible to pull this 'first letter' variable from the url string?
其次,是否可以从 url 字符串中提取这个“第一个字母”变量?
For example, Page 1 - There is a list of letters. The user clicks 'B' and the url is
例如,第 1 页 - 有一个字母列表。用户点击“B”,网址是
http://domain.com/page2.html?letter=b
http://domain.com/page2.html?letter=b
And page2 picks up that variable (b) and applies it to the Jquery, showing only those paragraphs
并且 page2 选取该变量 (b) 并将其应用于 Jquery,仅显示那些段落
回答by Aaron Harun
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:
您可以使用 PHP 来清理变量并在 JS 中打印它:
<script type="text/javascript">
var letter = '<?php echo (strlen($_GET['letter']) == 1) ? $_GET['letter'] : ''; ?>'
</script>
Or just grab it with document.locationand extract it.
或者只是抓住它document.location并提取它。
回答by BoltClock
If you'd like to use JavaScript to grab the letterfrom the URL query string, run a regular expression on window.location.search:
如果您想使用 JavaScriptletter从 URL 查询字符串中获取 ,请在 上运行正则表达式window.location.search:
var letterParam = window.location.search.match(/letter=([a-z])/i), letter;
if (letterParam)
{
letter = letterParam[1];
}
To match paragraphs starting with that letter, use the charAt()method in JavaScript strings:
要匹配以该字母开头的段落,请使用charAt()JavaScript 字符串中的方法:
if (letter)
{
$('p').each(function()
{
if ($(this).text().charAt(0).toUpperCase() == 'B')
{
// Apply the CSS class or change the style...
}
});
}
回答by John Rasch
To hide the <p>tags whose text does not start with the letter B:
要隐藏<p>文本不以字母开头的标签B:
$('p').filter(function() {
return $(this).text().charAt(0).toUpperCase() != 'B';
}).hide();
回答by Gabriele Petrioli
$('p').hide().filter(function(){return $(this).text().match(/^b/i);}).show();
or indented
或缩进
$('p').hide()
.filter(
function(){
return $(this).text().match(/^b/i);
}
)
.show();
remove the iin the match if you want it to be case sensitive..
i如果您希望它区分大小写,请删除匹配中的 ..
and you can look at http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.htmlon how to get the url parameters..
您可以查看http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html了解如何获取 url 参数..
回答by foxybagga
I don't know if its relevant here, but I am using this to style the first character.
我不知道它在这里是否相关,但我正在使用它来设置第一个字符的样式。
$(".menu ul li.page_item a").each(function() {
var text = $(this).html();
var first = $('<span>'+text.charAt(0)+'</span>').addClass('caps');
$(this).html(text.substring(1)).prepend(first);
});
cheers!
干杯!

