剥离JS文本并将结果保存到变量中
Strip JS text and save results to variable
我有以下JS函数来提取所有以" # "开头的单词并替换它们的css样式。我想添加一个url,重定向到包含标签的所有项目的查询。
如何保存剥离的标签,然后动态地将其添加到每个标签的url中?
$('.find-hashtag').each(function(index) {
var str = $(this).html();
var edt = str.replace(/(^|'s)(#[A-Za-z]+)/ig, "$1<a href='{% url "hashtagged_item_list” *stored_value_goes_here* %}'>$2</a>");
$(this).html(edt);
});
提前感谢您的帮助!
我希望完成什么:
<p class="find-hashtag">This is sample #html text.</p>
$('.find-hashtag').each(function(index) {
var str = $(this).html();
var tag = "html";
var edt = str.replace(/(^|'s)(#[A-Za-z]+)/ig, "$1<a href='{% url "hashtagged_item_list” tag %}'>$2</a>");
$(this).html(edt);
});
首先,用一个dummy标签存储django生成的url。因为django是服务器端,js是客户端,所以它们不能合并:
var url = '{% url "hashtagged_item_list" "dummy" %}';
然后,提取标签并用它替换'dummy':
var edt = str.replace(/'b#([A-Za-z]+)/ig, function(match, tag) {
return '<a href="' + url.replace('dummy', tag) + '">#' + tag + '</a>');
});
相关文章:
- 如何将getJson的响应保存在全局变量中
- 为所有查看网站的用户保存变量
- 关闭浏览器后保存变量
- 无法在Node.JS中保存变量
- 在 while 循环中保存变量
- 如何在移动到新的HTML页面后使用保存在Javascript文件中的保存变量
- PHP 会话不通过多个页面保存变量
- 保存变量
- 通过单击图像保存变量
- 在javascript中 - 保存变量作为参考
- 使用JavaScript onclick删除元素,并在SQL中保存变量
- 在cookie中保存变量- console.log返回具有会话id的变量
- JavaScript:在调用之间保存变量的值
- 如何在hta中保存变量,以便在关闭和打开hta后保存
- 在闭包中保存变量内容有问题
- 当html页面发生变化时,如何保存变量的状态
- 在闭包范围中保存变量
- Javascript 在使用 XMLHttpRequest 时通过作用域保存变量
- 在 cookie 中查找并保存变量
- 保存变量的值