javaScript变量中的更改是临时的,如何使其永久化.已附加控制台日志
Changein javaScript variable is temporary, how to i make it permanent. Console log is attached
我对p标记进行了更改,只有一个p发生了更改。后来,我再次尝试显示更改,但没有看到更改。
我在做什么:
$(document).ready(function(){
var html = "<div> <p></p> <p></p> <p></p> </div>";
//making some changes to <p> tag
$(html).find('p').each(function (){
$(this).text("SomeText");
console.log($(this).text());
});
// now again display the changes
$(html).find('p').each(function (){
console.log($(this).text());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>(Look in the console for the result.)</div>
之所以会发生这种情况,是因为您每次都从html
创建jquery对象。如果需要保留进度,则需要保存修改后的对象。
试试看:
$(document).ready(function(){
var html = "<div> <p></p> <p></p> <p></p> </div>";
//making some changes to <p> tag
var jHtml = $(html);
jHtml.find('p').each(function (){
$(this).text("SomeText");
console.log($(this).text());
});
// now again display the changes
jHtml.find('p').each(function (){
console.log($(this).text());
});
});
Jsfddle
您需要将更改后的HTML保存到某个位置。例如在DOM中或在变量中:
//making some changes to <p> tag
var newHTML = $(html).find('p').each(function (){
然后
// now again display the changes
$(newHTML).find('p').each(function (){
或者只创建jQuery HTML对象:
var html = $("<div> <p></p> <p></p> <p></p> </div>");
并将其用于您的方法:
html.find('p').each(
// etc
$(document).ready(function(){
var html = $("<div> <p></p> <p></p> <p></p> </div>");
//making some changes to <p> tag
html.find('p').each(function (){
$(this).text("SomeText");
console.log($(this).text());
});
// now again display the changes
html.find('p').each(function (){
console.log($(this).text());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>(Look in the console for the result.)</div>
相关文章:
- 如何使用Protractor从Chrome控制台获取所有日志
- 按压“;输入“;将值打印到控制台日志
- 执行JavaScript代码,使用selenium webdriver或WatiN从控制台获取日志
- jquery从url中获取参数也会在控制台日志中留下url
- 函数将信息返回到控制台日志,但实际上并没有执行应有的操作
- 将Function-Nothing日志记录返回到控制台
- 角度文本输入提交无法触发控制台日志
- 如何使用谷歌地理编码将地址转换为坐标后控制台日志数据
- 我的镀铬扩展程序没有't向控制台显示错误,但它显示日志
- 使用奇怪的控制台日志结果向输入日期添加天数
- 控制台日志未显示简单的$scope
- Ember.js——如何对模型进行控制台日志记录
- CSSLint:如何将控制台日志中的日志保存到文件中
- 在 PhantomJS 中在页面上下文中的控制台日志之间等待一段时间
- 有问题与承诺角度.控制台日志不适用于 HTTP 请求
- JavaScript控制台日志不起作用
- 如何在 Zkoss 中启用禁用的 chrome 控制台日志输出
- 如何在生产服务器上测试、调试和查看 Node js 应用的控制台日志消息
- 对函数变量的 JavaScript 控制台日志输出感到困惑
- 如何在safari中清除开发人员工具日志控制台?