Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将变量数组大写_Javascript_Jquery_Jquery Selectors - Fatal编程技术网

Javascript 将变量数组大写

Javascript 将变量数组大写,javascript,jquery,jquery-selectors,Javascript,Jquery,Jquery Selectors,我想把字符串的第一个字母大写。那部分已经完成了 我可以大写div.title元素和其他没有子节点的元素 我想做的是调用文本节点,只更改第一个文本节点 因此,我想将所有fcn对象转换为大写 我有以下html: <div class="title">Me & you <div class="subtitle">Me & you</div> </div> <br /> <div class="title">

我想把字符串的第一个字母大写。那部分已经完成了

我可以大写div.title元素和其他没有子节点的元素

我想做的是调用文本节点,只更改第一个文本节点

因此,我想将所有fcn对象转换为大写

我有以下html:

<div class="title">Me & you 
 <div class="subtitle">Me & you</div>
</div> 
<br /> 
<div class="title">Me & you</div>
我想利用fcn变量数组。尝试了各种方法但没有成功


有什么想法吗?

你考虑过使用CSS吗

.title, .subtitle {
    text-transform: capitalize;
}
如果要大写.title中的文本,而不是子节点,可以执行以下操作:

.title {
    text-transform: capitalize;
}
.title * {
    text-transform: none;
}

你考虑过使用CSS吗

.title, .subtitle {
    text-transform: capitalize;
}
如果要大写.title中的文本,而不是子节点,可以执行以下操作:

.title {
    text-transform: capitalize;
}
.title * {
    text-transform: none;
}

您可以使用
replaceWith
方法

您将看到我的示例没有将其中一个实例大写,即
me&You
。我认为这是因为筛选函数没有找到正确的文本节点。这里有一个关于如何查找文本节点的线程。我认为jQuery可能不是最好的方法

function capitalise(str) {
    if (!str) return;
    var stopWords = ['a', 'an', 'and', 'at', 'but', 'by', 'far', 'from', 'if', 'into', 'of', 'off', 'on', 'or', 'so', 'the', 'to', 'up'];
    str = str.replace(/\b\w+\b/ig, function(match) {
        return $.inArray(match, stopWords) == -1 ? match.substr(0, 1).toUpperCase() + match.substr(1) : match;
    }); 
    return str;
}

var fcn = $('div.title').contents().filter(function() { 
    return this.nodeType === 3;
}).replaceWith(function(){
    return capitalise($(this).text());
});
工作答案

根据我上面链接的帖子上的信息,我写了一些效果更好的东西。

函数大写(str){
如果(!str)返回;
var stopWords=['a','an','and','at','but','by','far','from','if','into','of','off','on','or','so','the','to','up'];
str=str.replace(/\b\w+\b/ig,函数(匹配){
返回$.inArray(match,stopWords)=-1?match.substr(0,1).toUpperCase()+match.substr(1):match;
}); 
返回str;
}
函数getTextNodesIn(节点,包括HiteSpaceNodes){
var textNodes=[],空格=/^\s*$/;
函数getTextNodes(节点){
if(node.nodeType==3){
if(包括hiteSpaceNodes | |!whitespace.test(node.nodeValue)){
textNodes.push(节点);
}
}否则{
对于(变量i=0,len=node.childNodes.length;i
您可以使用
replaceWith
方法

您将看到我的示例没有将其中一个实例大写,即
me&You
。我认为这是因为筛选函数没有找到正确的文本节点。这里有一个关于如何查找文本节点的线程。我认为jQuery可能不是最好的方法

function capitalise(str) {
    if (!str) return;
    var stopWords = ['a', 'an', 'and', 'at', 'but', 'by', 'far', 'from', 'if', 'into', 'of', 'off', 'on', 'or', 'so', 'the', 'to', 'up'];
    str = str.replace(/\b\w+\b/ig, function(match) {
        return $.inArray(match, stopWords) == -1 ? match.substr(0, 1).toUpperCase() + match.substr(1) : match;
    }); 
    return str;
}

var fcn = $('div.title').contents().filter(function() { 
    return this.nodeType === 3;
}).replaceWith(function(){
    return capitalise($(this).text());
});
工作答案

根据我上面链接的帖子上的信息,我写了一些效果更好的东西。

函数大写(str){
如果(!str)返回;
var stopWords=['a','an','and','at','but','by','far','from','if','into','of','off','on','or','so','the','to','up'];
str=str.replace(/\b\w+\b/ig,函数(匹配){
返回$.inArray(match,stopWords)=-1?match.substr(0,1).toUpperCase()+match.substr(1):match;
}); 
返回str;
}
函数getTextNodesIn(节点,包括HiteSpaceNodes){
var textNodes=[],空格=/^\s*$/;
函数getTextNodes(节点){
if(node.nodeType==3){
if(包括hiteSpaceNodes | |!whitespace.test(node.nodeValue)){
textNodes.push(节点);
}
}否则{
对于(变量i=0,len=node.childNodes.length;i
我的脚本仅大写第一个字母。所以这没用。每个单词的第一个字母。大写脚本就是这样做的。我现在只想使用它来大写div.title.So中的第一个文本节点,您想大写.title中的单词,而不是.subtitle中的单词?是的,因为在div.title元素上使用text()将删除subtitle div,但会大写文本。使用html()对我也不想要的“&”值进行编码。我的脚本只对第一个字母大写。所以这没用。每个单词的第一个字母。大写脚本就是这样做的。我现在只想使用它来大写div.title.So中的第一个文本节点,您想大写.title中的单词,而不是.subtitle中的单词?是的,因为在div.title元素上使用text()将删除subtitle div,但会大写文本。使用html()对我也不想要的“&”值进行编码。