Javascript 在ie中创建不同的动态链接
我的字符串:Javascript 在ie中创建不同的动态链接,javascript,jquery,regex,internet-explorer,split,Javascript,Jquery,Regex,Internet Explorer,Split,我的字符串: "<SPAN style=\"COLOR: #000000; PADDING-RIGHT: 30px\">Reason 1:</SPAN> My Text Here!" 其他浏览器: "Reason1BankbeatingexchangeratesCompareourratestoday" // Remove all characters, keep alphanumerical + spaces reasonTitleSpaces = reasonT
"<SPAN style=\"COLOR: #000000; PADDING-RIGHT: 30px\">Reason 1:</SPAN> My Text Here!"
其他浏览器:
"Reason1BankbeatingexchangeratesCompareourratestoday"
// Remove all characters, keep alphanumerical + spaces
reasonTitleSpaces = reasonTitle.replace(/[^A-Za-z0-9\s]+/g, '');
// Remove all characters, keep alphanumerical
reasonTitle = reasonTitle.replace(/[^A-Za-z0-9]+/g, '');
您可以使用jQuery API创建链接,而不是将其作为字符串进行操作 这将为您提供更好的结果和跨浏览器兼容性 而不是
reasonTitle=$(this.html()代码>,克隆DOM结构,以便您可以在不更改页面的情况下自由操作:
<script>
reasonTitle = $(this).clone();
//Remove the span tag, now you have only the reason
reasonTitle.find('span').remove()
//Get the text value
reasonTitle = $.trim(reasonTitle.text());
//Create the anchor
anchorLink = $("<a />",{id:'anchor', name:reasonTitle})
$(this).parent().before(anchorLink);
//You don't need to count your `<li>`, use `<ol>` for ordinal lists
//Create the link:
$("<a />",{href:'#'+reasonTitle}).click(function(){
_gaq.push(['_trackEvent', experimentConversionReference, 'ReasonClicked', $(this).text()]);
}).text(reasonTitle );
</script>
reasonTitle=$(this.clone();
//删除span标记,现在您只有原因了
reasonTitle.find('span').remove()
//获取文本值
reasonTitle=$.trim(reasonTitle.text());
//创建锚定
anchorLink=$(“”,{id:'anchor',名称:reasonTitle})
$(this).parent()。在(锚点链接)之前;
//你不需要计算你的``,用``表示有序列表
//创建链接:
$(“”,{href:'#'+reasonTitle})。单击(函数(){
_gaq.push([''u trackEvent',experimentConversionReference',ReasonClicked',$(this.text());
}).文本(标题);
我知道它很脏,但它很管用
// Remove extras for IE
reasonTitle = reasonTitle.split("Reason").slice(1).join("Reason");
reasonTitleSpaces = reasonTitleSpaces.split("Reason").slice(1).join("Reason");
// Remove any extra occurances of "span" for IE
reasonTitle = reasonTitle.replace("SPAN","");
reasonTitleSpaces = reasonTitleSpaces.replace("SPAN","");
能否提供$(this.html()
的原始值。我不知道你想做什么,但HTML的字符串计算永远不是答案。添加了一个例子@RoryMcCrossanSo我正在从90%的时间都是相同格式的页面中获取动态内容,并从中创建链接。@Anicho:你希望“原因1:”
来自该示例?不,我正在修剪原因,删除:
并将其替换为1。
// Remove extras for IE
reasonTitle = reasonTitle.split("Reason").slice(1).join("Reason");
reasonTitleSpaces = reasonTitleSpaces.split("Reason").slice(1).join("Reason");
// Remove any extra occurances of "span" for IE
reasonTitle = reasonTitle.replace("SPAN","");
reasonTitleSpaces = reasonTitleSpaces.replace("SPAN","");