javascript prototypal extend

本文介绍了一种在网页上实现内联编辑功能的方法,包括字段和区域的编辑。通过JavaScript实现了元素之间的转换,使用户可以方便地在查看模式和编辑模式间切换,并保存更改。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="jquery-1.6.2.js"></script>
<script type="text/javascript">
	function id() {
		var elements = [];
		for ( var i = 0, len = arguments.length; i < len; ++i) {
			var element = arguments[i];
			if (typeof element === 'string') {
				element = document.getElementById(element);
			}
			if (arguments.length === 1) {
				return element;
			}
			elements.push(element);
		}
		return elements;
	}
	function clone(object) {
		function F() {
		}
		F.prototype = object;
		return new F;
	}

	function addEvent(el, type, fn) {
		if (window.addEventListener) {
			el.addEventListener(type, fn, false);
		} else if (window.attachEvent) {
			el.attachEvent('on' + type, fn);
		} else {
			el['on' + type] = fn;
		}
	}

	var EditInPlaceField = {
		configure : function(id, parent, value) {
			this.id = id;
			this.value = value || 'defalut value';
			this.parentElement = parent;

			this.createElements(this.id);
			this.attachEvents();
		},
		createElements : function(id) {
			this.containerElement = document.createElement('div');
			this.parentElement.appendChild(this.containerElement);

			this.staticElement = document.createElement('span');
			this.containerElement.appendChild(this.staticElement);
			this.staticElement.innerHTML = this.value;

			this.fieldElement = document.createElement('input');
			this.fieldElement.type = 'text';
			this.fieldElement.value = this.value;
			this.containerElement.appendChild(this.fieldElement);

			this.saveButton = document.createElement('input');
			this.saveButton.type = 'button';
			this.saveButton.value = 'Save';
			this.containerElement.appendChild(this.saveButton);

			this.cancelButton = document.createElement('input');
			this.cancelButton.type = 'button';
			this.cancelButton.value = 'Cancel';
			this.containerElement.appendChild(this.cancelButton);

			this.convertToText();
		},
		attachEvents : function() {
			var that = this;
			addEvent(this.staticElement, 'click', function() {
				that.convertToEditable();
			});
			addEvent(this.saveButton, 'click', function() {
				that.save();
			});
			addEvent(this.cancelButton, 'click', function() {
				that.cancel();
			});
		},
		convertToEditable : function() {
			this.staticElement.style.display = 'none';
			this.fieldElement.style.display = 'inline';
			this.saveButton.style.display = 'inline';
			this.cancelButton.style.display = 'inline';

			this.setValue(this.value);
		},
		save : function() {
			this.value = this.getValue();
			var that = this;
			var callback = {
				success : function() {
					that.convertToText();
				},
				failure : function() {
					alert('Error saving value.');
				}
			}
			this.convertToText();
		},
		cancel : function() {
			this.convertToText();
		},
		convertToText : function() {
			this.fieldElement.style.display = 'none';
			this.saveButton.style.display = 'none';
			this.cancelButton.style.display = 'none';
			this.staticElement.style.display = 'inline';

			this.setValue(this.value);
		},
		setValue : function(value) {
			this.fieldElement.value = value;
			this.staticElement.innerHTML = value;
		},
		getValue : function() {
			return this.fieldElement.value;
		}
	}

	var EditInPlaceArea = clone(EditInPlaceField);

	EditInPlaceArea.createElements = function(id) {
		this.containerElement = document.createElement('div');
		this.parentElement.appendChild(this.containerElement);

		this.staticElement = document.createElement('p');
		this.containerElement.appendChild(this.staticElement);
		this.staticElement.innerHTML = this.value;

		this.fieldElement = document.createElement('textarea');
		this.fieldElement.value = this.value;
		this.containerElement.appendChild(this.fieldElement);

		this.saveButton = document.createElement('input');
		this.saveButton.type = 'button';
		this.saveButton.value = 'Save';
		this.containerElement.appendChild(this.saveButton);

		this.cancelButton = document.createElement('input');
		this.cancelButton.type = 'button';
		this.cancelButton.value = 'Cancel';
		this.containerElement.appendChild(this.cancelButton);

		this.convertToText();
	};
	EditInPlaceArea.convertToText = function() {
		this.fieldElement.style.display = 'none';
		this.saveButton.style.display = 'none';
		this.cancelButton.style.display = 'none';
		this.staticElement.style.display = 'block';

		this.setValue(this.value);
	};

	EditInPlaceArea.convertToEditable = function() {
		this.staticElement.style.display = 'none';
		this.fieldElement.style.display = 'block';
		this.saveButton.style.display = 'inline';
		this.cancelButton.style.display = 'inline';

		this.setValue(this.value);
	};

	$(document).ready(function() {
		var field = clone(EditInPlaceField);
		field.configure('field', id('div1'), 'field value');
		$("#btn1").click(function() {
			alert(field.getValue());
		});
		var area = clone(EditInPlaceArea);
		area.configure('area', id('div2'), 'area value');
		$("#btn2").click(function() {
			alert(area.getValue());
		});
	});
</script>
</head>
<body>
	<h2>3 page</h2>
	<hr>
	<div id="div1"></div>
	<input type="button" id="btn1" value="btn1">
	<div id="div2"></div>
	<input type="button" id="btn2" value="btn2">
	<br>

</body>
</html>

资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在C#开发中,Windows Media Player控件是集成音频和视频播放功能的强大工具。本文将介绍如何在C#中实现不同的播放模式,如随机播放、列表循环和单曲循环,这些功能在多媒体应用中十分常见 。 要使用Windows Media Player控件,首先需要将其添加到C#项目中。在Visual Studio中,可以通过在工具箱中搜索“Windows Media Player”,并将其拖放到窗体上完成 。接着,设置控件的基本属性,如URL,以指定要播放的媒体文件 。 随机播放模式会在一首歌曲播放结束后,随机选择播放列表中的下一首歌曲。可以通过创建一个包含所有歌曲URL的数组,并利用Random类生成随机索引来实现。例如: 列表循环模式会在一首歌曲播放结束后,自动从播放列表的开头重新开始播放。实现方法是检测到播放结束后,将URL重置为列表的第一个元素: 单曲循环模式则是在一首歌曲播放结束后,重新播放当前歌曲。可以通过将播放器的当前播放位置重置为0并重新播放来实现: 以上代码均需在windowsMediaPlayer1_PlayStateChange事件处理器中实现,该事件会在播放器的播放状态改变时触发 。需要注意的是,这些示例假设已正确引用了WMPLib命名空间,并且Windows Media Player控件的ID为“windowsMediaPlayer” 。 在实际应用中,除了实现播放模式外,还可能需要考虑错误处理、用户界面更新等因素。为了使播放列表更具动态性,可以考虑从数据库或XML文件加载歌曲信息,而不是硬编码在代码中,从而提升用户体验 。通过这些方法,可以在C#中灵活实现Windows Media Player的各种播放模式,满足不同多媒体应用场景的需求 。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值