我正在使用一小段JS来为我的blockquotes添加一个表示元素(如此处所示):
<script type="text/javascript">
$('blockquote.message').append('<span class="arrow" />');
</script>
Run Code Online (Sandbox Code Playgroud)
但W3C验证器讨厌这个废话:
文档类型不允许元素"span"在这里
我究竟做错了什么?有没有办法纠正这个错误,同时保持功能?
谢谢!
在$ .ajax函数中,url部分有data.json这是一个文本文件,但我想放一个url即
代码适用
$(document).ready(function() {
$('#content').html('');
$.ajax({
url:'data.json',
dataType: "json",
success: function(data) {
$('#content').append('<p>'+data.rank+'</p>');
}
});});
Run Code Online (Sandbox Code Playgroud)
其中data.json是一个文本文件...但是我将'data.json'替换为' http://twittercounter.com/api/username=Anand_Dasgupta&output=json&results=3 '...这是实际的url,然后没有输出......
$(document).ready(function() {
$('#content').html('');
$.ajax({
url:'http://twittercounter.com/api/username=Anand_Dasgupta&output=json&results=3',
dataType: "json",
success: function(data) {
$('#content').append('<p>'+data.rank+'</p>');
}
});});
Run Code Online (Sandbox Code Playgroud)
建议将受到高度赞赏.谢谢.
我的下面的代码创建了一个txt文件并将一些内容写入该文件.但是当我多次运行脚本时,我需要在前面的行之后写一个新行.码:
string filePath = "D:\\DOT_NET\\C#\\abc.txt";
FileInfo t = new FileInfo(filePath);
StreamWriter Tex = t.CreateText();
Tex.WriteLine("Hi freinds");
Tex.WriteLine("csharpfriends is the new url for c-sharp");
Tex.Write(Tex.NewLine);
Tex.Close();
Run Code Online (Sandbox Code Playgroud)
abc.txt文件的当前输出:
Hi friends
csharpfriends is the new url for c-sharp
Run Code Online (Sandbox Code Playgroud)
但是如果我多次运行脚本,我需要输出:
Hi friends
csharpfriends is the new url for c-sharp
Hi friends
csharpfriends is the new url for c-sharp
Hi friends
csharpfriends is the new url for c-sharp
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?请帮忙.
我有这个HTML:
<div id="wrapper">
<div class="inner">Inner</div>
<div class="inner">Inner</div>
<div class="inner">Inner</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我想追加这一行
<div id="first">first</div>
Run Code Online (Sandbox Code Playgroud)
在第一个div#wrapper和第一个 之间div.inner- 所以即使没有元素,它也将始终是第一个元素.
所以它看起来像这样:
<div id="wrapper">
<div id="first">first</div>
<div class="inner">Inner</div>
<div class="inner">Inner</div>
<div class="inner">Inner</div>
</div>
Run Code Online (Sandbox Code Playgroud)
或者如果我没有.innerdiv,它将如下所示:
<div id="wrapper">
<div id="first">first</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我怎么做?谢谢,Alon
任何人都可以解释什么被认为是附加在JavaScript中的最佳做法?
var element = document.createElement('p');
element.innerHTML = 'This is appended' ;
var div = document.getElementById('div');
div.appendChild(element);
Run Code Online (Sandbox Code Playgroud)
要么
var div = document.getElementById('div');
div.innerHTML = '<p>This is appended</p>';
Run Code Online (Sandbox Code Playgroud)
我看到很多人在jQuery中这样做:
$('#div').append('<p>This is appended</p>');
Run Code Online (Sandbox Code Playgroud)
但我总是这样做:
var element = document.createElement('p');
element.innerHTML = 'This is appended' ;
$('#div').append(element);
Run Code Online (Sandbox Code Playgroud)
先感谢您
我想知道在避免重复的同时创建列表的最佳方法是什么.
我在mysql中有一些数据包含产品类型.
例如:
id ------- category
1 -------- food, drink, vege
2 -------- food, drink
3 -------- vege, baby goods
4 -------- fish
Run Code Online (Sandbox Code Playgroud)
我瞄准的输出是:
['food','drink','vege','baby goods','fish']
Run Code Online (Sandbox Code Playgroud)
(请注意订单对我来说无关紧要)
数据集有超过40,000条记录,因此手动检查肯定不是一种选择......
如果你能给我一个说明或建议,我将不胜感激.
有人可以解释一下以下行为吗?
X=2*[[]]
print X
X[0].append(1)
print X
Run Code Online (Sandbox Code Playgroud)
产量
[[], []]
[[1], [1]]
Run Code Online (Sandbox Code Playgroud)
我希望最后的清单是[[1], []].的确如下
X=[[],[]]
print X
X[0].append(1)
print X
Run Code Online (Sandbox Code Playgroud)
产量
[[], []]
[[1], []]
Run Code Online (Sandbox Code Playgroud)
为何如此区别?
我试图提取"first_name"并"last_name"从信息的长字符串看起来像这样:
{
"123123123": {
"id": "12321312****",
"email": "***************",
"first_name": "Marcus",
"gender": "male",
"last_name": "Bengtsson",
"link": "https://www.facebook.com/app_scoped_user_id/123123123/",
"locale": "en_EN",
"middle_name": "Peter",
"name": "Marcus Peter Bengtsson"
}
}
Run Code Online (Sandbox Code Playgroud)
我的方式做到这一点(这可能是可怕的错误和一个非常糟糕的解决方案)是我首先从提取的子串"first_name"来"link"使用此代码:
String subStr = str.substring(str.indexOf("first_name"), str.lastIndexOf("link"));
Run Code Online (Sandbox Code Playgroud)
然后我得到:
first_name":"Marcus","gender":"male","last_name":"Bengtsson","
然后,我做同样的事情,但是从":"以"gender"获得"first_name:
String firstNameOfUser = subStr.substring(subStr.indexOf(":")+2, subStr.lastIndexOf("gender")-3);
Run Code Online (Sandbox Code Playgroud)
然后同样的事情"last_name":
String lastNameOfUser = subStr.substring(subStr.indexOf(""last_name"")+12, subStr.lastIndexOf(",")-1);
Run Code Online (Sandbox Code Playgroud)
最后,我在两个字符串之间添加一个空格:
String nameOfUser = new StringBuilder().append(firstNameOfUser).append(" ").append(lastNameOfUser).toString();
Run Code Online (Sandbox Code Playgroud)
然后我得到:
Marcus Bengtsson
可能有一个更好的方法来做到这一点,但我无法找出如何解决.
我想打开一个文件,读取其内容,然后在文件中附加一行.我以为我应该使用"a +"标志来完成任务.
我有一个打开文件并返回指向该文件的指针的函数.
FILE* open_weekly_disk_file(char* filename){
FILE* weekly_log;
weekly_log = fopen(filename, "a+");
//weekly_log = fopen(filename, "r");
if(! weekly_log){
printf("The attempt to open the weekly log failed!\n");
return NULL;
} else{
return weekly_log;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个函数调用上面的函数并使用scanf从文件中读取内容:
void sample_function(char* filename){
FILE* log;
char token[100], current_read[100];
int limit;
log = opened_weekly_disk_file(filename);
// The problem happens here
for(limit=0; limit < TOKEN_NUMBER; limit++){
if(fscanf(log, "%s%s", &token, ¤t_read) == 2){
printf("%s %s\n", token, current_read);
}
}
...
}
Run Code Online (Sandbox Code Playgroud)
我使用时此代码有效:
weekly_log = fopen(filename, "r");
Run Code Online (Sandbox Code Playgroud)
但是当我将"r"标志更改为"a +"时不起作用.我在for循环之前得到了一个Segmentation错误.
我下载了Google的Python练习,这是问题之一:
给定一个字符串列表,返回字符串长度为2或更大且字符串的第一个和最后一个字符相同的字符串数的计数
我对此的回答如下:
def match_ends(words):
a=[]
for word in words:
if len(word)>=2 and word[0]==word[-1]:
a=a.append(word)
return len(a)
Run Code Online (Sandbox Code Playgroud)
但是,对于此列表:
words=['aba', 'xyz', 'aa', 'x', 'bbb']
Run Code Online (Sandbox Code Playgroud)
它只返回“ 1”。我不明白,为什么append在转弯时不添加所有与之匹配的字符串。
这是Google的解决方案:
def match_ends(words):
count = 0
for word in words:
if len(word) >= 2 and word[0] == word[-1]:
count = count + 1
return count
Run Code Online (Sandbox Code Playgroud)