2014-10-28 142 views
-2

我需要从字符串中删除HTML元素。Javascript/jquery:从字符串获取元素

任何建议,请留下。

+0

你能表现出更多的代码? – 2014-10-28 11:24:48

+1

@ Stuart.Sklinar当然,pastebin传入! – 2014-10-28 11:26:24

+0

难道你不能在这里添加(或者至少更多)吗?它给了整个事情更多的上下文....? – 2014-10-28 11:28:43

回答

2

您可以删除表中直接使用格式字符串substr()

var myString = "smth <table>asdasdasd</table>smth else"; 
var firstPart = myString.substr(0,myString.indexOf('<table>')); 
var secondPart = myString.substr(myString.lastIndexOf('</table>')+8); 
myString = firstPart + secondPart; 

Fiddle

UPD并从DOM移除台我会或者使用jQuery的方法:

$(document).ready(function(){ 
    $('#tableId').remove(); 
}) 

或纯JS:

var table = document.getElementById('tableId'); 
table.parentNode.removeChild(table); 
+0

这应该工作,我会试试看。 – 2014-10-28 11:45:58

2

删除表似乎对我来说还好。你等待DOM加载,你可以使用$(文件)。就绪(),或在window.onload

jsfiddle

window.onload = function() { 
    var table = document.getElementById("fileTable"); 
    table.innerHTML = 'I removed the table'; 
    console.log(table); 
} 
1

看完原来你有一个大括号缺少全码:

<script type="text/javascript"> 
     function privateComment(){ 
     var file = 0; 
     var message = document.getElementById("commentPrivate").innerHTML; 
     if(message.indexOf("contenteditable") > -1){ 
      var table = document.getElementById("fileTable"); 

      var type = table.getAttribute("type"); 
      var file_id = table.getAttribute("fileId"); 
      var taskId = table.getAttribute("taskId"); 
      table.innerHTML = ''; 
      var string = string + '{"type": "' + type + '", "file_id": "' + file_id + '", "taskId": "' + taskId + '"}'; 
      var file = 1; 
      } // <<< THIS IS NOT THE LAST BRACE 
     var taskId = document.getElementById('form-task-title').getAttribute('showing'); 
     var button = document.getElementById("submit-private-comment"); 
     alert(message); 
    } //<<ADD THIS BRACE . 

    </script> 
+0

感谢您的帮助,但在pastebin的底部我放置了“//在此处切断了代码”。下次我会更清楚一点。再次感谢! – 2014-10-28 11:53:35

+0

NP - 如果您要提供代码,请提供完整的上下文 - 或将其创建为小型,以便其他人可以将其提取并运行(我花了大约20分钟才能将它们合并到一起!) – 2014-10-28 12:23:24