Tôi có yếu tố HTML chứa này:Làm cách nào để xóa thêm khoảng trắng bằng javascript hoặc jquery?
<!--Product Style--> <div style="float: right; padding-top: 4px; padding-bottom: 5px;"> P6C245RO </div> <div style="text-transform: uppercase; font-weight: bold; padding-top: 4px; padding-bottom: 5px;"> Style </div> <div style="clear: both; border-top: 1px solid rgb(216, 216, 216); padding-top: 4px;"> <!--Product Description--> <div style="font-size: 11px ! important;"></div> <div style="background: url("http://ii.armaniexchange.com/ArmaniExchange/images/en_US/global/globalgraphics/bullet.gif") no-repeat scroll 0pt 4px transparent; padding-left: 12px;">fine tonal striped fabric</div> <div style="background: url("http://ii.armaniexchange.com/ArmaniExchange/images/en_US/global/globalgraphics/bullet.gif") no-repeat scroll 0pt 4px transparent; padding-left: 12px;">epaulettes and sleeve tab</div> <div style="background: url("http://ii.armaniexchange.com/ArmaniExchange/images/en_US/global/globalgraphics/bullet.gif") no-repeat scroll 0pt 4px transparent; padding-left: 12px;">metal logo plate on the chest pocket</div>
Khi tôi đọc nó sử dụng jquery tôi nhận được .text()
chứa rất nhiều không gian và/n ở giữa các văn bản nhưng không có các thẻ html.
Làm cách nào để xóa tất cả các khoảng trắng này và trả lại văn bản rõ ràng bằng javascript tinh khiết hoặc jquery?
1 Tôi hiểu lắm câu hỏi. Điều đó sẽ làm việc tốt cho không gian và dòng mới (những gì OP cho biết ông cần). Lưu ý rằng một số trình duyệt không thực hiện đúng '\ s' một cách chính xác khi bạn nhận được vào khoảng trống không phải ASCII (http://thinkweb2.com/projects/prototype/whitespace-deviations/), nhưng đó là một niggle. :-) –
Bạn đang thiếu 'g' (chỉ báo chung). – user113716
@patrick Có, sau khi thêm 'g' (chỉ số toàn cục), mọi thứ hoạt động hoàn hảo. –