Cách xóa tệp css trong phản ứng

Mọi tệp JavaScript hoặc CSS bên ngoài, dù được thêm thủ công hay tự động, đều có thể bị xóa khỏi trang. Tuy nhiên, kết quả cuối cùng có thể không hoàn toàn như những gì bạn nghĩ. Tôi sẽ nói về điều này một chút sau

Tự động xóa tệp JavaScript hoặc CSS bên ngoài

Để xóa tệp JavaScript hoặc CSS bên ngoài khỏi một trang, trước tiên, điều quan trọng là tìm kiếm chúng bằng cách duyệt qua DOM, sau đó gọi phương thức removeChild[] của DOM để thực hiện công việc đó. Một cách tiếp cận chung là xác định một tệp bên ngoài cần xóa dựa trên tên tệp của nó, mặc dù chắc chắn có những cách tiếp cận khác, chẳng hạn như theo tên lớp CSS. Với ý nghĩ đó, hàm bên dưới sẽ xóa mọi tệp JavaScript hoặc CSS bên ngoài dựa trên tên tệp đã nhập

function removejscssfile[filename, filetype]{
	var targetelement=[filetype=="js"]? "script" : [filetype=="css"]? "link" : "none" //determine element type to create nodelist from
	var targetattr=[filetype=="js"]? "src" : [filetype=="css"]? "href" : "none" //determine corresponding attribute to test for
	var allsuspects=document.getElementsByTagName[targetelement]
	for [var i=allsuspects.length; i>=0; i--]{ //search backwards within nodelist for matching elements to remove
	if [allsuspects[i] && allsuspects[i].getAttribute[targetattr]!=null && allsuspects[i].getAttribute[targetattr].indexOf[filename]!=-1]
		allsuspects[i].parentNode.removeChild[allsuspects[i]] //remove element by calling parentNode.removeChild[]
	}
}

removejscssfile["somescript.js", "js"] //remove all occurences of "somescript.js" on page
removejscssfile["somestyle.css", "css"] //remove all occurences "somestyle.css" on page

Chức năng bắt đầu bằng cách tạo một bộ sưu tập trong số tất cả các thành phần "SCRIPT" hoặc "LINK" trên trang tùy thuộc vào loại tệp mong muốn để xóa. Thuộc tính tương ứng để xem xét cũng thay đổi tương ứng [thuộc tính "

function createjscssfile[filename, filetype]{
	if [filetype=="js"]{ //if filename is a external JavaScript file
		var fileref=document.createElement['script']
		fileref.setAttribute["type","text/javascript"]
		fileref.setAttribute["src", filename]
	}
	else if [filetype=="css"]{ //if filename is an external CSS file
		var fileref=document.createElement["link"]
		fileref.setAttribute["rel", "stylesheet"]
		fileref.setAttribute["type", "text/css"]
		fileref.setAttribute["href", filename]
	}
	return fileref
}

function replacejscssfile[oldfilename, newfilename, filetype]{
	var targetelement=[filetype=="js"]? "script" : [filetype=="css"]? "link" : "none" //determine element type to create nodelist using
	var targetattr=[filetype=="js"]? "src" : [filetype=="css"]? "href" : "none" //determine corresponding attribute to test for
	var allsuspects=document.getElementsByTagName[targetelement]
	for [var i=allsuspects.length; i>=0; i--]{ //search backwards within nodelist for matching elements to remove
		if [allsuspects[i] && allsuspects[i].getAttribute[targetattr]!=null && allsuspects[i].getAttribute[targetattr].indexOf[oldfilename]!=-1]{
			var newelement=createjscssfile[newfilename, filetype]
			allsuspects[i].parentNode.replaceChild[newelement, allsuspects[i]]
		}
	}
}

replacejscssfile["oldscript.js", "newscript.js", "js"] //Replace all occurences of "oldscript.js" with "newscript.js"
replacejscssfile["oldstyle.css", "newstyle", "css"] //Replace all occurences "oldstyle.css" with "newstyle.css"
0" hoặc "
function createjscssfile[filename, filetype]{
	if [filetype=="js"]{ //if filename is a external JavaScript file
		var fileref=document.createElement['script']
		fileref.setAttribute["type","text/javascript"]
		fileref.setAttribute["src", filename]
	}
	else if [filetype=="css"]{ //if filename is an external CSS file
		var fileref=document.createElement["link"]
		fileref.setAttribute["rel", "stylesheet"]
		fileref.setAttribute["type", "text/css"]
		fileref.setAttribute["href", filename]
	}
	return fileref
}

function replacejscssfile[oldfilename, newfilename, filetype]{
	var targetelement=[filetype=="js"]? "script" : [filetype=="css"]? "link" : "none" //determine element type to create nodelist using
	var targetattr=[filetype=="js"]? "src" : [filetype=="css"]? "href" : "none" //determine corresponding attribute to test for
	var allsuspects=document.getElementsByTagName[targetelement]
	for [var i=allsuspects.length; i>=0; i--]{ //search backwards within nodelist for matching elements to remove
		if [allsuspects[i] && allsuspects[i].getAttribute[targetattr]!=null && allsuspects[i].getAttribute[targetattr].indexOf[oldfilename]!=-1]{
			var newelement=createjscssfile[newfilename, filetype]
			allsuspects[i].parentNode.replaceChild[newelement, allsuspects[i]]
		}
	}
}

replacejscssfile["oldscript.js", "newscript.js", "js"] //Replace all occurences of "oldscript.js" with "newscript.js"
replacejscssfile["oldstyle.css", "newstyle", "css"] //Replace all occurences "oldstyle.css" with "newstyle.css"
1"]. Sau đó, hàm bắt đầu lặp lại các phần tử được thu thập ngược lại để xem liệu có phần tử nào khớp với tên của tệp cần xóa không. Có một lý do cho hướng đảo ngược - nếu/bất cứ khi nào một phần tử được xác định bị xóa, bộ sưu tập sẽ thu gọn một phần tử mỗi lần và để tiếp tục quay vòng qua bộ sưu tập mới một cách chính xác, việc đảo ngược hướng sẽ thực hiện thủ thuật [nó có thể gặp phải các phần tử không xác định, . Bây giờ, để xóa phần tử đã xác định, phương thức DOM
function createjscssfile[filename, filetype]{
	if [filetype=="js"]{ //if filename is a external JavaScript file
		var fileref=document.createElement['script']
		fileref.setAttribute["type","text/javascript"]
		fileref.setAttribute["src", filename]
	}
	else if [filetype=="css"]{ //if filename is an external CSS file
		var fileref=document.createElement["link"]
		fileref.setAttribute["rel", "stylesheet"]
		fileref.setAttribute["type", "text/css"]
		fileref.setAttribute["href", filename]
	}
	return fileref
}

function replacejscssfile[oldfilename, newfilename, filetype]{
	var targetelement=[filetype=="js"]? "script" : [filetype=="css"]? "link" : "none" //determine element type to create nodelist using
	var targetattr=[filetype=="js"]? "src" : [filetype=="css"]? "href" : "none" //determine corresponding attribute to test for
	var allsuspects=document.getElementsByTagName[targetelement]
	for [var i=allsuspects.length; i>=0; i--]{ //search backwards within nodelist for matching elements to remove
		if [allsuspects[i] && allsuspects[i].getAttribute[targetattr]!=null && allsuspects[i].getAttribute[targetattr].indexOf[oldfilename]!=-1]{
			var newelement=createjscssfile[newfilename, filetype]
			allsuspects[i].parentNode.replaceChild[newelement, allsuspects[i]]
		}
	}
}

replacejscssfile["oldscript.js", "newscript.js", "js"] //Replace all occurences of "oldscript.js" with "newscript.js"
replacejscssfile["oldstyle.css", "newstyle", "css"] //Replace all occurences "oldstyle.css" with "newstyle.css"
4 được gọi trên nó

Vậy điều gì thực sự xảy ra khi bạn xóa tệp JavaScript hoặc CSS bên ngoài? . Trong trường hợp JavaScript, trong khi phần tử bị xóa khỏi cây tài liệu, bất kỳ mã nào được tải dưới dạng một phần của tệp JavaScript bên ngoài vẫn còn trong bộ nhớ của trình duyệt. Điều đó có nghĩa là, bạn vẫn có thể truy cập các biến, hàm, v.v. đã được thêm vào khi tệp bên ngoài được tải lần đầu [ít nhất là trong IE7 và Firefox 2. x]. Nếu bạn đang tìm cách lấy lại bộ nhớ trình duyệt bằng cách xóa JavaScript bên ngoài, đừng dựa vào thao tác này để thực hiện tất cả công việc của bạn. Với các tệp CSS bên ngoài, khi bạn xóa một tệp, tài liệu sẽ chạy lại để tính đến các quy tắc CSS đã xóa, nhưng thật không may, không phải trong IE7 [Firefox 2. x và Opera 9 làm]

Thử nghiệm. "bản thảo của tôi. js" nguồn

var petname="Spotty"
alert["Tên thú cưng. " + petname]

"phong cách của tôi. css" nguồn

#demotable td{
màu nền. vàng nhạt;
}

#demotable b{
màu. xanh da trời;
}

Tự động thay thế tệp JavaScript hoặc CSS bên ngoài

Việc thay thế một tệp JavaScript hoặc CSS bên ngoài không khác nhiều so với việc xóa một tệp trong suốt quá trình. Thay vì gọi

function createjscssfile[filename, filetype]{
	if [filetype=="js"]{ //if filename is a external JavaScript file
		var fileref=document.createElement['script']
		fileref.setAttribute["type","text/javascript"]
		fileref.setAttribute["src", filename]
	}
	else if [filetype=="css"]{ //if filename is an external CSS file
		var fileref=document.createElement["link"]
		fileref.setAttribute["rel", "stylesheet"]
		fileref.setAttribute["type", "text/css"]
		fileref.setAttribute["href", filename]
	}
	return fileref
}

function replacejscssfile[oldfilename, newfilename, filetype]{
	var targetelement=[filetype=="js"]? "script" : [filetype=="css"]? "link" : "none" //determine element type to create nodelist using
	var targetattr=[filetype=="js"]? "src" : [filetype=="css"]? "href" : "none" //determine corresponding attribute to test for
	var allsuspects=document.getElementsByTagName[targetelement]
	for [var i=allsuspects.length; i>=0; i--]{ //search backwards within nodelist for matching elements to remove
		if [allsuspects[i] && allsuspects[i].getAttribute[targetattr]!=null && allsuspects[i].getAttribute[targetattr].indexOf[oldfilename]!=-1]{
			var newelement=createjscssfile[newfilename, filetype]
			allsuspects[i].parentNode.replaceChild[newelement, allsuspects[i]]
		}
	}
}

replacejscssfile["oldscript.js", "newscript.js", "js"] //Replace all occurences of "oldscript.js" with "newscript.js"
replacejscssfile["oldstyle.css", "newstyle", "css"] //Replace all occurences "oldstyle.css" with "newstyle.css"
4, bạn sẽ sử dụng
function createjscssfile[filename, filetype]{
	if [filetype=="js"]{ //if filename is a external JavaScript file
		var fileref=document.createElement['script']
		fileref.setAttribute["type","text/javascript"]
		fileref.setAttribute["src", filename]
	}
	else if [filetype=="css"]{ //if filename is an external CSS file
		var fileref=document.createElement["link"]
		fileref.setAttribute["rel", "stylesheet"]
		fileref.setAttribute["type", "text/css"]
		fileref.setAttribute["href", filename]
	}
	return fileref
}

function replacejscssfile[oldfilename, newfilename, filetype]{
	var targetelement=[filetype=="js"]? "script" : [filetype=="css"]? "link" : "none" //determine element type to create nodelist using
	var targetattr=[filetype=="js"]? "src" : [filetype=="css"]? "href" : "none" //determine corresponding attribute to test for
	var allsuspects=document.getElementsByTagName[targetelement]
	for [var i=allsuspects.length; i>=0; i--]{ //search backwards within nodelist for matching elements to remove
		if [allsuspects[i] && allsuspects[i].getAttribute[targetattr]!=null && allsuspects[i].getAttribute[targetattr].indexOf[oldfilename]!=-1]{
			var newelement=createjscssfile[newfilename, filetype]
			allsuspects[i].parentNode.replaceChild[newelement, allsuspects[i]]
		}
	}
}

replacejscssfile["oldscript.js", "newscript.js", "js"] //Replace all occurences of "oldscript.js" with "newscript.js"
replacejscssfile["oldstyle.css", "newstyle", "css"] //Replace all occurences "oldstyle.css" with "newstyle.css"
1 để thực hiện việc đặt giá thầu

function createjscssfile[filename, filetype]{
	if [filetype=="js"]{ //if filename is a external JavaScript file
		var fileref=document.createElement['script']
		fileref.setAttribute["type","text/javascript"]
		fileref.setAttribute["src", filename]
	}
	else if [filetype=="css"]{ //if filename is an external CSS file
		var fileref=document.createElement["link"]
		fileref.setAttribute["rel", "stylesheet"]
		fileref.setAttribute["type", "text/css"]
		fileref.setAttribute["href", filename]
	}
	return fileref
}

function replacejscssfile[oldfilename, newfilename, filetype]{
	var targetelement=[filetype=="js"]? "script" : [filetype=="css"]? "link" : "none" //determine element type to create nodelist using
	var targetattr=[filetype=="js"]? "src" : [filetype=="css"]? "href" : "none" //determine corresponding attribute to test for
	var allsuspects=document.getElementsByTagName[targetelement]
	for [var i=allsuspects.length; i>=0; i--]{ //search backwards within nodelist for matching elements to remove
		if [allsuspects[i] && allsuspects[i].getAttribute[targetattr]!=null && allsuspects[i].getAttribute[targetattr].indexOf[oldfilename]!=-1]{
			var newelement=createjscssfile[newfilename, filetype]
			allsuspects[i].parentNode.replaceChild[newelement, allsuspects[i]]
		}
	}
}

replacejscssfile["oldscript.js", "newscript.js", "js"] //Replace all occurences of "oldscript.js" with "newscript.js"
replacejscssfile["oldstyle.css", "newstyle", "css"] //Replace all occurences "oldstyle.css" with "newstyle.css"

Lưu ý hàm trợ giúp

function createjscssfile[filename, filetype]{
	if [filetype=="js"]{ //if filename is a external JavaScript file
		var fileref=document.createElement['script']
		fileref.setAttribute["type","text/javascript"]
		fileref.setAttribute["src", filename]
	}
	else if [filetype=="css"]{ //if filename is an external CSS file
		var fileref=document.createElement["link"]
		fileref.setAttribute["rel", "stylesheet"]
		fileref.setAttribute["type", "text/css"]
		fileref.setAttribute["href", filename]
	}
	return fileref
}

function replacejscssfile[oldfilename, newfilename, filetype]{
	var targetelement=[filetype=="js"]? "script" : [filetype=="css"]? "link" : "none" //determine element type to create nodelist using
	var targetattr=[filetype=="js"]? "src" : [filetype=="css"]? "href" : "none" //determine corresponding attribute to test for
	var allsuspects=document.getElementsByTagName[targetelement]
	for [var i=allsuspects.length; i>=0; i--]{ //search backwards within nodelist for matching elements to remove
		if [allsuspects[i] && allsuspects[i].getAttribute[targetattr]!=null && allsuspects[i].getAttribute[targetattr].indexOf[oldfilename]!=-1]{
			var newelement=createjscssfile[newfilename, filetype]
			allsuspects[i].parentNode.replaceChild[newelement, allsuspects[i]]
		}
	}
}

replacejscssfile["oldscript.js", "newscript.js", "js"] //Replace all occurences of "oldscript.js" with "newscript.js"
replacejscssfile["oldstyle.css", "newstyle", "css"] //Replace all occurences "oldstyle.css" with "newstyle.css"
2, về cơ bản chỉ là một bản sao của
function createjscssfile[filename, filetype]{
	if [filetype=="js"]{ //if filename is a external JavaScript file
		var fileref=document.createElement['script']
		fileref.setAttribute["type","text/javascript"]
		fileref.setAttribute["src", filename]
	}
	else if [filetype=="css"]{ //if filename is an external CSS file
		var fileref=document.createElement["link"]
		fileref.setAttribute["rel", "stylesheet"]
		fileref.setAttribute["type", "text/css"]
		fileref.setAttribute["href", filename]
	}
	return fileref
}

function replacejscssfile[oldfilename, newfilename, filetype]{
	var targetelement=[filetype=="js"]? "script" : [filetype=="css"]? "link" : "none" //determine element type to create nodelist using
	var targetattr=[filetype=="js"]? "src" : [filetype=="css"]? "href" : "none" //determine corresponding attribute to test for
	var allsuspects=document.getElementsByTagName[targetelement]
	for [var i=allsuspects.length; i>=0; i--]{ //search backwards within nodelist for matching elements to remove
		if [allsuspects[i] && allsuspects[i].getAttribute[targetattr]!=null && allsuspects[i].getAttribute[targetattr].indexOf[oldfilename]!=-1]{
			var newelement=createjscssfile[newfilename, filetype]
			allsuspects[i].parentNode.replaceChild[newelement, allsuspects[i]]
		}
	}
}

replacejscssfile["oldscript.js", "newscript.js", "js"] //Replace all occurences of "oldscript.js" with "newscript.js"
replacejscssfile["oldstyle.css", "newstyle", "css"] //Replace all occurences "oldstyle.css" with "newstyle.css"
3 như đã thấy ở trang trước, nhưng được sửa đổi để trả về phần tử mới được tạo thay vì thực sự thêm phần tử đó vào trang. Nó có ích khi
function createjscssfile[filename, filetype]{
	if [filetype=="js"]{ //if filename is a external JavaScript file
		var fileref=document.createElement['script']
		fileref.setAttribute["type","text/javascript"]
		fileref.setAttribute["src", filename]
	}
	else if [filetype=="css"]{ //if filename is an external CSS file
		var fileref=document.createElement["link"]
		fileref.setAttribute["rel", "stylesheet"]
		fileref.setAttribute["type", "text/css"]
		fileref.setAttribute["href", filename]
	}
	return fileref
}

function replacejscssfile[oldfilename, newfilename, filetype]{
	var targetelement=[filetype=="js"]? "script" : [filetype=="css"]? "link" : "none" //determine element type to create nodelist using
	var targetattr=[filetype=="js"]? "src" : [filetype=="css"]? "href" : "none" //determine corresponding attribute to test for
	var allsuspects=document.getElementsByTagName[targetelement]
	for [var i=allsuspects.length; i>=0; i--]{ //search backwards within nodelist for matching elements to remove
		if [allsuspects[i] && allsuspects[i].getAttribute[targetattr]!=null && allsuspects[i].getAttribute[targetattr].indexOf[oldfilename]!=-1]{
			var newelement=createjscssfile[newfilename, filetype]
			allsuspects[i].parentNode.replaceChild[newelement, allsuspects[i]]
		}
	}
}

replacejscssfile["oldscript.js", "newscript.js", "js"] //Replace all occurences of "oldscript.js" with "newscript.js"
replacejscssfile["oldstyle.css", "newstyle", "css"] //Replace all occurences "oldstyle.css" with "newstyle.css"
1 được gọi trong
function createjscssfile[filename, filetype]{
	if [filetype=="js"]{ //if filename is a external JavaScript file
		var fileref=document.createElement['script']
		fileref.setAttribute["type","text/javascript"]
		fileref.setAttribute["src", filename]
	}
	else if [filetype=="css"]{ //if filename is an external CSS file
		var fileref=document.createElement["link"]
		fileref.setAttribute["rel", "stylesheet"]
		fileref.setAttribute["type", "text/css"]
		fileref.setAttribute["href", filename]
	}
	return fileref
}

function replacejscssfile[oldfilename, newfilename, filetype]{
	var targetelement=[filetype=="js"]? "script" : [filetype=="css"]? "link" : "none" //determine element type to create nodelist using
	var targetattr=[filetype=="js"]? "src" : [filetype=="css"]? "href" : "none" //determine corresponding attribute to test for
	var allsuspects=document.getElementsByTagName[targetelement]
	for [var i=allsuspects.length; i>=0; i--]{ //search backwards within nodelist for matching elements to remove
		if [allsuspects[i] && allsuspects[i].getAttribute[targetattr]!=null && allsuspects[i].getAttribute[targetattr].indexOf[oldfilename]!=-1]{
			var newelement=createjscssfile[newfilename, filetype]
			allsuspects[i].parentNode.replaceChild[newelement, allsuspects[i]]
		}
	}
}

replacejscssfile["oldscript.js", "newscript.js", "js"] //Replace all occurences of "oldscript.js" with "newscript.js"
replacejscssfile["oldstyle.css", "newstyle", "css"] //Replace all occurences "oldstyle.css" with "newstyle.css"
5 để thay thế phần tử cũ bằng phần tử mới. Một số tin tốt ở đây - khi bạn thay thế một tệp CSS bên ngoài bằng một tệp CSS khác, tất cả các trình duyệt, bao gồm cả IE7, sẽ tự động điều chỉnh lại tài liệu để tính đến các quy tắc CSS của tệp mới

Thử nghiệm. "kịch bản cũ. js" nguồn

var petname="Spotty"
alert["Tên thú cưng. " + petname]

"kịch bản mới. js" nguồn

var petname="Người đẹp"
alert["Tên thú cưng. " + petname]

"phong cách cũ. css" nguồn

#demotable2 td{
màu nền. vàng nhạt;
}

#demotable2 b{
màu. xanh da trời;
}

"phong cách mới. css" nguồn

#demotable2 td{
màu nền. xanh nhạt;
}

#demotable2 b{
màu. màu đỏ;
}

Phần kết luận

Vì vậy, khi tất cả những điều này hữu ích? . Vui vẻ tìm hiểu chúng là gì hoặc thực hiện kỹ thuật. . ]

Tôi có thể xóa các tệp CSS không?

Bạn không thể xóa tệp CSS thông qua Javascript trên trình duyệt vì đó là ngôn ngữ kịch bản phía máy khách . Bạn có thể xóa các kiểu css được áp dụng trên các phần tử html của mình, vâng; .

Làm cách nào để xóa CSS nhập?

#Phương thức 1. Bằng cách kết hợp các tệp CSS . Điều này có nghĩa là bạn chỉ cần sao chép và dán trực tiếp các tệp bạn đang nhập vào tệp gốc. Điều này sẽ loại bỏ nhu cầu nhập tệp.

Làm cách nào để xóa CSS trong JS?

Sử dụng kiểu. removeProperty[] để xóa thuộc tính kiểu CSS khỏi phần tử , e. g. hộp. Phong cách. removeProperty['màu nền'].

Các tệp CSS được lưu trữ trong React ở đâu?

Bạn có thể tạo một tệp CSS mới trong thư mục dự án của bạn và thêm CSS của bạn vào trong đó. Sau đó, bạn có thể nhập tệp vào trang thành phần, lớp hoặc React JS của mình.

Chủ Đề