for para in file.paragraphs:
raw_words += para.text #把每段的单词添加到字符串里面
raw_words = raw_words.lower()
#print(raw_words)
words = re.findall('[a-z]+',raw_words) #得到所有的英文单词,排除了汉语单词,各种符号
return words
修改后的代码
#读取docx中的文本代码示例
import docx
def docx_file():
file=docx.Document(filename)
raw_words = '' #保存所有的单词
#输出每一段的内容
for para in file.paragraphs:
raw_words += para.text #把每段的单词添加到字符串里面
raw_words = raw_words.lower()
#print(raw_words)
words = re.findall('[a-z]+',raw_words) #得到所有的英文单词,排除了汉语单词,各种符号
return words
替换文件名就可以了,def docx_file():
选择器速度提升<br>选择器的速度大幅度提高了,下表为jQuery1.1.2和1.1.3的选择器速度对比,提高了8倍多<br>Browser jQuery 1.1.2 jQuery 1.1.3 % Improvement <br>IE 6 4890ms 661ms 740% <br>Firefox 2 5629ms 567ms 993% <br>Safari 2 3575ms 475ms 753% <br>Opera 9.1 3196ms 326ms 980% <br>Average improvement: 867% <br><br>下表为jQuery1.1.3与常用的一些JS库选择器的对比:<br>Browser Prototype jQuery Mootools Ext Dojo <br>IE 6 1476ms 661ms 1238ms 672ms 738ms <br>Firefox 2 219ms 567ms 220ms 951ms 440ms <br>Safari 2 1568ms 475ms 909ms 417ms 527ms <br>Opera 9.1 220ms 326ms 217ms 296ms 220ms <br><br><br>更新项目<br><br><br>New Selectors<br>Unicode Selectors: This is a huge addition for those of you who want to use Unicode
attribute
values, IDs, class names, or tag names. You can now use them directly in jQuery selectors:<br><br>$("div.台北")<br>$("div#台北")<br>$("foo_bar台北")<br>$("div[@id=台北]")Escape Selectors: A frequently requested feature you can now select elements by ID (or other selector) that uses a special character, for example this will find the div that has the ID of “foo.bar”:<br><br>$("div#foo\\.bar")Inequality Selector: While this selector isn’t part of the CSS specification, it’s frequently used and included in other selector libraries, so we decided to add it in:<br><br>$("div[@id!=test]"):nth-child() improvements: This selector allows you to locate specific child elements. We’ve supported selectors like :nth-child(1) and :nth-child(odd) since the beginning of jQuery, now we’ve added advanced :nth-child selectors, such as:<br><br>$("div:nth-child(2n)")<br>$("div:nth-child(2n+1)")<br>$("div:nth-child(n)")Space-separated
attribute
s: After being removed in jQuery 1.0, this selector has now been brought back by popular demand. It allows you to locate individual items in a space-separated
attribute
(such as a class or rel
attribute
).<br><br>$("a[@rel~=test]")Animation Improvements<br><br>参数: <br>options <br>返回值: <br>XMLHttpRequest <br>使用HTTP请求一个页面。<br>这是jQuery的低级AJAX实现。要查看高级抽象,见$.set、$.post等,这些方法更易于理解和使用。但是功能上有限制(例如,没有错误处理函数)。<br>警告:如果数据类型指定为"script",那么POST自动转化为GET方法。(因为script会作为一个嵌入页面的script标签进行载入) <br>$.ajax()函数返回它创建的XMLHttpRequest对象。在大部分情况下,你不需要直接操作此对象。通常,这个XMLHttpRequest对象主要用于需要手动中断XMLHttpRequest请求的时候。 <br>注意:如果你指明了下面列出的数据类型,请确保服务端发送了正确的MIME响应类型(如. xml 的类型是 "text/xml")。错误的MIME类型能够导致脚本出现意想不到的问题。请查看AJAX的范例来了解数据类型的更多信息。 <br>$.ajax()函数需要一个参数,一个包含有键/值对的对象,用于初始化并操作请求对象。 <br>在jQuery 1.2版本中,如果你指明了一个JSONP回调函数,你就可以从其它的域中载入JSON类型的数据,写法类似于 "myurl?callback=?" 。jQuery会自动调用正确的方法名称来代替查询字符串,执行你指定的回调函数。或者,你也可以指定jsonp的数据类型的回调函数,此函数会自动添加到Ajax请求中。 <br>参数选项: <br>async(true) 数据类型: Boolean <br>默认情况下,所有的请求都是异步发送的(默认为true)。 如果需要发送同步请求, 设置选项为false。注意,同步请求可能会暂时的锁定浏览器, 当请求激活时不能进行任何操作。 <br>beforeSend 数据类型:
Function
<br>一个预处理函数用于在发送前修改XMLHttpRequest对象,设置自定义头部等。 XMLHttpRequest作为惟一的参数被传递。这是一个 Ajax 事件。
function
(XMLHttpRequest) {<br> this; // the options for this ajax request<br>}cache(true) 数据类型: Boolean <br>jQuery 1.2中新添加的参数, 如果设为false,则会强制浏览器不缓存请求的页面。 <br>complete 数据类型:
Function
<br>当请求完成时执行的函数(在成功或失败之后执行)。这个函数有2个参数: XMLHttpRequest对象和一个描述HTTP相应的状态字符串。 这是一个 Ajax 事件。
function
(XMLHttpRequest, textStatus) {<br> this; // the options for this ajax request<br>}contentType("application/x-www-form-urlencoded") 数据类型: String <br>发送到服务器的数据的内容类型。默认是 "application/x-www-form-urlencoded", 适合大多数情况。 <br>data 数据类型:
Object
,String <br>要发送给服务器的数据。如果不是字符串,那么它会被转化为一个查询字符串。在GET请求中它被添加到url的末尾。要防止这种自动转化,请查看processData选项。 数据对象必须是一组键/值对。如果键对应的值是数组,jQuery会将其值赋给同一个键属性。 例如 {foo:["bar1", "bar2"]} 变为 '&foo=bar1&foo=bar2'。 <br>dataType( Intelligent Guess (xml or html)) 数据类型: String <br>期待由服务器返回值类型。如果没有明确指定,jQuery会根据实际返回的MIME类型自动的将responseXML或responseText传递给success指定的回调函数。有效的类型(返回的类型的结果值会作为第一个参数传递给success指定的回调函数)有: "xml": 返回一个可以由jQuery处理的XML文档。 <br>"html": 返回文本格式的HTML代码。包括求值后的脚本标记。 <br>"script": 将响应作为Javascript语句求值,并返回纯文本。不缓存此脚本,除非设置了cache选项。设置为"script"类型会将post方法转换为get方法。 <br>"json": 将响应作为JSON求值,并返回一个Javascript对象。 <br>"jsonp": 使用JSONP载入一个JSON代码块. 会在URL的末尾添加"?callback=?"来指明回调函数。(jQuery 1.2以上的版本支持) <br>"text": 文本格式的字符串 <br>error 数据类型:
Function
<br>请求失败时执行的函数。函数具有3个参数: XMLHttpRequest对象,一个描述产生的错误类型和一个可选的异常对象, 如果有的化。 这是一个Ajax 事件。
function
(XMLHttpRequest, textStatus, errorThrown) {<br> // typically only one of textStatus or errorThrown <br> // will have info<br> this; // the options for this ajax request<br>}global(true) 数据类型: Boolean <br>是否为当前的请求触发全局AJAX事件处理函数,默认值为true。设置为false可以防止触发像ajaxStart或ajaxStop这样的全局事件处理函数。这可以用于控制多个不同的Ajax事件。 <br>ifModified(false) 数据类型: Boolean <br>只有响应自上次请求后被修改过才承认是成功的请求。是通过检查头部的Last-Modified值实现的。默认值为false,即忽略对头部的检查 <br>jsonp 数据类型: String <br>在jsonp请求中重新设置回调的函数。这个值用于代替'callback=?'中的查询字符串。'callback=?'位于get请求中url的末尾或是post请求传递的数据中。因此设置 {jsonp:'onJsonPLoad'} 会将 'onJsonPLoad=?' 传送给服务器。 <br>processData(true) 数据类型: Boolean <br>在默认的情况下,如果data选项传进的数据是一个对象而不是字符串,将会自动地被处理和转换成一个查询字符串,以适应默认的content-type--"application/x-www-form-urlencoded"。如果想发送DOM
Document
s,就要把这个选项设置为false。 <br>success 数据类型:
Function
<br>当请求成功时调用的函数。这个函数会得到二个参数:从服务器返回的数据(根据“dataType”进行了格式化)和一个描述HTTP相应的状态字符串。这是一个 Ajax 事件。
function
(data, textStatus) {<br> // data could be xmlDoc, jsonObj, html, text, etc...<br> this; // the options for this ajax request<br>}timeout 数据类型: Number <br>如果通过$.ajaxSetup设置了一个全局timeout,那么此函数使用一个局部timeout覆盖了全局timeout(单位为毫秒)。例如,你可以设置比较长的延迟给一个特殊的请求,同时其他所有请求使用1秒的延迟。有关全局延迟,见$.ajaxTimeout()。 <br>type("GET") 数据类型: String <br>请求的类型 ("POST" 或 "GET"), 默认是 "GET"。注意:其他的HTTP请求方法,如PUT和DELETE,在这里也可以使用,当时它们并不被所有的浏览器支持。 <br>url(The current page) 数据类型: String <br>请求发送的目标URL地址 <br>username 数据类型: String <br>username可用于在响应一个HTTP连接时的认证请求。 实例 <br>载入并执行一个JavaScript文件。 <br>$.ajax({<br> type: "GET",<br> url: "test.js",<br> dataType: "script"<br>});保存数据到服务器,完成后通知用户。 <br> $.ajax({<br> type: "POST",<br> url: "some.php",<br> data: "name=John&location=Boston",<br> success:
function
(msg){<br> alert( "Data Saved: " + msg );<br> }<br> });取得一个HTML页面的最新版本。 <br>$.ajax({<br> url: "test.html",<br> cache: false,<br> success:
function
(html){<br> $("#results").append(html);<br> }<br>});同步载入数据。在执行请求的时候阻塞浏览器。这是在保证数据的同步性比交互更重要的情况下的一种更好的方法。 <br> var html = $.ajax({<br> url: "some.php",<br> async: false<br> }).responseText;向服务器发送xml文档数据。通过设置processData选项为false,将数据自动转换为string的动作被禁止了。 <br>var xml
Document
= [create xml
document
];<br> $.ajax({<br> url: "page.php",<br> processData: false,<br> data: xml
Document
,<br> success: handleResponse<br> });load( url, [data], [callback] )参数: <br>url (String): 装入页面的URL地址。 <br>params (Map): (可选)发送到服务端的键/值对参数。 <br>callback (
Function
): (可选) 当数据装入完成时执行的函数.
function
(responseText, textStatus, XMLHttpRequest) {<br> this; // dom element<br>}返回值: <br>jQuery <br>装入一个远程HTML内容到一个DOM结点。 默认使用get方法发送请求,但如果指定了额外的参数,将会使用post方法发送请求。在 jQuery 1.2中,可以在URL参数中指定一个jQuery选择器,这会过滤返回的HTML文档,只取得文档中匹配选择器的元素。此语法类似于"url #some > selector"。 <br>实例 <br>载入文档的sidebar的导航部分到一个无序列表中。 <br>$("#links").load("/Main_Page #p-Getting-Started li");将feeds.html文件载入到id为feeds的div中。 <br>$("#feeds").load("feeds.html");同上,但是发送了附加的参数,并且在响应结束后执行一个自定义函数。 <br> $("#feeds").load("feeds.php", {limit: 25},
function
(){<br> alert("The last 25 entries in the feed have been loaded");<br> });jQuery.get( url, [data], [callback] )参数: <br>url (String): 装入页面的URL地址 <br>Map(可选): (可选)发送到服务端的键/值对参数 <br>callback (
Function
): (可选) 当远程页面装入完成时执行的函数
function
(data, textStatus) {<br> // data可以是xmlDoc, jsonObj, html, text, 等...<br> this; // the options for this ajax request<br>}返回值: <br>XMLHttpRequest <br>使用GET请求一个页面。 <br>这是向服务器发送get请求的简单方法。它可以指定一个回调函数,在请求完成后执行(只有在请求成功时)。如果还需要设置error和success回调函数,则需要使用$.ajax。 <br>实例 <br>请求test.php页,忽略返回值. <br>$.get("test.php");请求test.php页并发送附加数据(忽略返回值). <br>$.get("test.php", { name: "John", time: "2pm" } );显示从test.php请求的返回值(HTML 或 XML, 根据不同返回值). <br>$.get("test.php",
function
(data){<br> alert("Data Loaded: " + data);<br>});显示向test.cgi发送附加数据请求的返回值 (HTML 或 XML, 根据不同返回值). <br>$.get("test.cgi", { name: "John", time: "2pm" },<br>
function
(data){<br> alert("Data Loaded: " + data);<br> });jQuery.getJSON( url, [data], [callback] )参数: <br>url (String): 装入页面的URL地址 <br>Map(可选): (可选)发送到服务端的键/值对参数 <br>callback (
Function
): (可选) 当数据装入完成时执行的函数
function
(data, textStatus) {<br> // data will be a jsonObj<br> this; // the options for this ajax request<br>}返回值: <br>XMLHttpRequest <br>使用GET请求JSON数据。 <br>在jQuery 1.2版本中,如果你指明了一个JSONP回调函数,你就可以从其它的域中载入JSON类型的数据,写法类似于 "myurl?callback=?" 。jQuery会自动调用正确的方法名称来代替查询字符串,执行你指定的回调函数。或者,你也可以指定jsonp的数据类型的回调函数,此函数会自动添加到Ajax请求中。注意: 请记住, that lines after this
function
will be executed before callback. <br>实例 <br>从Flickr JSONP API中载入最新的四幅猫的图片 <br>$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?",<br>
function
(data){<br> $.each(data.items,
function
(i,item){<br> $("<img/>").attr("src", item.media.m).appendTo("#images");<br> if ( i == 3 ) return false;<br> });<br> });从test.js载入JSON数据, 从返回的JSON数据读取name值。 <br>$.getJSON("test.js",
function
(json){<br> alert("JSON Data: " + json.users[3].name);<br>});从test.js载入JSON数据, 传递一个附加参数,从返回的JSON数据读取name值。 <br>$.getJSON("test.js", { name: "John", time: "2pm" },
function
(json){<br> alert("JSON Data: " + json.users[3].name);<br>});显示向test.php发送请求的返回值 (HTML 或 XML, 根据不同返回值). <br>$.getIfModified("test.php",
function
(data){<br> alert("Data Loaded: " + data);<br>});显示向test.php发送请求的返回值 (HTML 或 XML, 根据不同返回值),提供了一个附加的参数. <br>$.getIfModified("test.php", { name: "John", time: "2pm" },<br>
function
(data){<br> alert("Data Loaded: " + data);<br> });列出从pages.php返回的查询结果,将返回的数组转化为一段HTML代码。 <br>var id=$("#id").attr("value");<br> $.getJSON("pages.php",{id:id},dates);<br>
function
dates(datos)<br>{ <br> $("#list").html("Name:"+datos[1].name+"<br>"+"Last Name:"+datos[1].lastname+"<br>"+"Address:"+datos[1].address);<br>}jQuery.getScript( url, [callback] )参数: <br>url (String): 装入页面的URL地址 <br>callback (
Function
): (可选) 当数据装入完成时执行的函数
function
(data, textStatus) {<br> // data应该是javascript<br> this; // the options for this ajax request<br>}返回值: <br>XMLHttpRequest <br>使用GET请求JavaScript文件并执行。 <br>在jQuery 1.2前, getScript只能从页面所在的主机载入脚本,1.2中, 你可以从任何主机载入脚本。警告: Safari 2 及其更老的版本不能在全局上下文中正确识别脚本。如果你通过getScript载入函数,请保证设置一个延迟来执行这个脚本。 <br>实例 <br>我们动态的载入一个新的官方jQuery颜色动画插件,载入后绑定一些动画效果到元素上。 <br>$.getScript("http://dev.jquery.com/view/trunk/plugins/color/jquery.color.js",
function
(){<br> $("#go").click(
function
(){<br> $(".block").animate( { backgroundColor: 'pink' }, 1000)<br> .animate( { backgroundColor: 'blue' }, 1000);<br> });<br>});载入test.js JavaScript文件并执行。 <br>$.getScript("test.js");载入test.js JavaScript文件并执行,当执行结束后显示一条警告信息。 <br>$.getScript("test.js",
function
(){<br> alert("Script loaded and executed.");<br>});jQuery.post( url, [data], [callback], [type] )参数: <br>url (String): 装入页面的URL地址 <br>Map(可选): (可选)发送到服务端的键/值对参数 <br>callback (
Function
): (可选) 当数据装入完成时执行的函数
function
(data, textStatus) {<br> // data可能是 xmlDoc, jsonObj, html, text, 等...<br> this; // the options for this ajax request<br>}String $.postJSON =
function
(url, data, callback) {<br> $.post(url, data, callback, "json");<br>};返回值: <br>XMLHttpRequest <br>使用POST请求一个页面。 <br>这是向服务器发送post请求的简单方法。它可以指定一个回调函数,在请求完成后执行(只有在请求成功时)。如果还需要设置error和success回调函数,则需要使用$.ajax。 <br>ajaxComplete( callback )参数: <br>callback (
Function
): 要执行的函数
function
(event, XMLHttpRequest, ajaxOptions) {<br> this; // dom element listening<br>}返回值: <br>jQuery <br>当一个AJAX请求结束后,执行一个函数。这是一个Ajax事件 <br>实例 <br>当AJAX请求完成时显示一条信息。 <br>$("#msg").ajaxComplete(
function
(request, settings){<br> $(this).append("<li>Request Complete.</li>");<br> });ajaxError( callback )参数: <br>callback (
Function
): 要执行的函数
function
(event, XMLHttpRequest, ajaxOptions, thrownError) {<br> // thrownError only passed if an error was caught<br> this; // dom element listening<br>}返回值: <br>jQuery <br>当一个AJAX请求失败后,执行一个函数。这是一个Ajax事件. <br>实例 <br>当AJAX请求错误时显示一条信息。 <br> $("#msg").ajaxError(
function
(request, settings){<br> $(this).append("<li>Error requesting page " + settings.url + "</li>");<br> });ajaxSend( callback )参数: <br>callback (
Function
): 要执行的函数
function
(event, XMLHttpRequest, ajaxOptions) {<br> this; // dom element listening<br>}返回值: <br>jQuery <br>在一个AJAX请求发送时,执行一个函数。这是一个Ajax事件. <br>实例 <br>当AJAX请求发出后显示一条信息。 <br> $("#msg").ajaxSend(
function
(evt, request, settings){<br> $(this).append("<li<Starting request at " + settings.url + "</li<");<br> });ajaxStart( callback )参数: <br>callback (
Function
): 要执行的函数
function
() {<br> this; // dom element listening<br>}返回值: <br>jQuery <br>在一个AJAX请求开始但还没有激活时,执行一个函数。这是一个Ajax事件. <br>实例 <br>当AJAX请求开始(并还没有激活时)显示loading信息。 <br>$("#loading").ajaxStart(
function
(){<br> $(this).show();<br> });ajaxStop( callback )参数: <br>callback (
Function
): 要执行的函数
function
() {<br> this; // dom element listening<br>}返回值: <br>jQuery <br>当所有的AJAX都停止时,执行一个函数。这是一个Ajax事件. <br>实例 <br>当所有AJAX请求都停止时,隐藏loading信息。 <br>$("#loading").ajaxStop(
function
(){<br> $(this).hide();<br> });ajaxSuccess( callback )参数: <br>callback (
Function
): 要执行的函数
function
(event, XMLHttpRequest, ajaxOptions) {<br> this; // dom element listening<br>}返回值: <br>jQuery <br>当一个AJAX请求成功完成后,执行一个函数。这是一个Ajax事件 <br>实例 <br>当AJAX请求成功完成时,显示信息。 <br> $("#msg").ajaxSuccess(
function
(evt, request, settings){<br> $(this).append("<li>Successful Request!</li>");<br> });jQuery.ajaxSetup( options )参数: <br>Options: 用于Ajax请求的键/值对 <br>为所有的AJAX请求进行全局设置。查看$.ajax函数取得所有选项信息。 <br>实例 <br>设置默认的全局AJAX请求选项。 <br>$.ajaxSetup({<br> url: "/xmlhttp/",<br> global: false,<br> type: "POST"<br>});<br>$.ajax({ data: myData });serialize( )返回值: <br>jQuery <br>以名称和值的方式连接一组input元素。返回值类似于: single=Single2&multiple=Multiple&multiple=Multiple3&radio=radio2 。在jQuery 1.2中。serialize方法实现了正确表单元素序列,而不再需要插件支持。 <br>实例 <br>连接表单元素的一组查询字符串,可用于发送Ajax请求。 <br>
function
showValues() {<br> var str = $("form").serialize();<br> $("#results").text(str);<br> }<br><br> $(":checkbox, :radio").click(showValues);<br> $("select").change(showValues);<br> showValues();serializeArray( )返回值: <br>jQuery <br>连接所有的表单和表单元素(类似于.serialize()方法),但是返回一个JSON数据格式。 <br>实例 <br>从form中取得一组值,显示出来 <br>
function
showValues() {<br> var fields = $(":input").serializeArray();<br> alert(fields);<br> $("#results").empty();<br> jQuery.each(fields,
function
(i, field){<br> $("#results").append(field.value + " ");<br> });<br> }<br><br> $(":checkbox, :radio").click(showValues);<br> $("select").change(showValues);<br> showValues();
Tips and Tricks Internet Development Index
--------------------------------------------------------------------------------
As with any type of programming, writing bug-free, efficient scripts that meet your expectations takes a bit of work. The following sections provide some tips and hints to make that work take less time and go more smoothly.
Checking the Internet Explorer Version Number
Canceling a Button Click
Preventing a
Document
From Being Cached
Using
Object
s
Replacing Custom Controls with DHTML
Checking the Internet Explorer Version Number
You should always check for the type and version of the client browser, so that your content degrades gracefully if the client browser does not support features on your Web site.
The easiest way to identify a browser and its characteristics (browser code name, version number, language, etc.) in script is through the Dynamic HTML (DHTML)?A HREF="
object
s/obj_navigator.html">navigator
object
. You can also access this
object
and its properties in C++ applications through the IOmNavigator interface.
The userAgent property of the navigator
object
returns a string that includes the browser and browser version. The following example Microsoft® JScript®
function
runs on most browsers and returns the version number for any Microsoft Internet Explorer browser and zero for all other browsers.
SHOWExample
function
msieversion()
// Return Microsoft Internet Explorer (major) version number, or 0 for others.
// This
function
works by finding the "MSIE " string and extracting the version number
// following the space, up to the semicolon
var ua = window.navigator.userAgent
var msie = ua.indexOf ( "MSIE " )
if ( msie > 0 ) // is Microsoft Internet Explorer; return version number
return parseFloat ( ua.substring ( msie+5, ua.indexOf ( ";", msie ) ) )
return 0 // is other browser
When checking browser version numbers, always check for version numbers greater than or equal to a target version. In this way, your Web site will be be compatible with future versions of the browser. For example, if you have designed your content for the latest version of Internet Explorer, use that version number as a minimum version number.
Note Browsers often have several releases of a browser version. For example, 4.01, 5.0, 5.5 and 6.0b are all different versions of Internet Explorer. The 'b' in 6.0b represents a beta version of Internet Explorer 6.
As of Internet Explorer 5, conditional comments are available as an alternative technique for detecting browser versions. Conditional comments have the advantage of not using a script block, which means that it is not always necessary to use scripting and DHTML when working with conditional comments. When no scripting is used in a Web page, no scripting engine needs to be loaded. Conditional comments are processed during the downloading and parsing phase, so only the content that is targeted for the browser loading the Web page is actually downloaded. Conditional comments can be combined freely with other browser detection techniques. For more information, see About Conditional Comments.
Canceling a Button Click
The following HTML example shows a common scripting mistake related to event handling and canceling the default action.
SHOWExample
<HEAD><TITLE>Canceling the Default Action</TITLE>
<SCRIPT LANGUAGE="JScript">
function
askConfirm()
{ return window.confirm ("Choose OK to follow hyperlink, Cancel to
not.")
</SCRIPT>
<BODYonload="b3.onclick=askConfirm">
<!-- Try links with different hookups - should be canceled by "Cancel" to confirm dialog. -->
<BR><A NAME=b1 HREF="http://www.microsoft.com" onclick="askConfirm()">1 Without return (won't work)</A>
<BR><A NAME=b2 HREF="http://www.microsoft.com" onclick="return askConfirm()">2 With return (works)</A>
<BR><A NAME=b3 HREF="http://www.microsoft.com">3
Function
pointer (works)</A>
</BODY>
</HTML>
The first a element in this example does not work properly. Without the return in the onclick燡Script expression, the browser interprets the
function
expression, throws away the resulting value, and leaves the default action unaffected.
The other a elements correctly bind the return value to the event, hence the default action can be canceled when false is returned.
Preventing a
Document
From Being Cached
You can prevent a
document
from being cached by adding the following meta tag to the
document
.
<META HTTP-EQUIV="Expires" CONTENT="0">
Preventing the
document
from being cached ensures that a fresh copy of the
document
will always be retrieved from the site, even during the user's current session, regardless of how the user has set the browser's caching options. This is useful if the content of the
document
changes frequently.
Using
Object
s
Object
s are Microsoft® ActiveX® Controls or other similar components that provide custom capabilities and services for HTML
document
s. You can add a control to your
document
using the
object
element, and you can gain access to the capabilities and services of the control using its properties and methods from script.
When using
object
s, be aware that DHTML extends every
object
by providing these additional properties:
align classid code
codeBase codeType
data form
height name
object
recordset
type width
If a control has properties with these same names, you will not be able to access the properties unless you preface the name with the
object
property. For example, assume that an ActiveX control is added to the
document
using the following:
<
OBJECT
ID="MyControl" HEIGHT=100 WIDTH=200 CLASSID="clsid: ... ">
</PARAM NAME="width" VALUE="400">
</
OBJECT
>
In this example, there are two widths: an extended property set within the
object
element, and a property belonging to the control that is set using the param element. To access these from script, you use the following code.
alert(MyControl.width); // this is Dynamic HTML's property; displays "200"
alert(MyControl.
object
.width); // this is the
object
's property; displays "400"
Replacing Custom Controls with DHTML
DHTML provides everything you need to generate animated effects without resorting to custom controls. For example, consider the following script, which is a replacement for the Path control.
SHOWExample
var tickDuration;
tickDuration = 50;
var active
Object
Count;
var active
Object
s;
var itemDeactivated;
var tickGeneration;
active
Object
s = new Array();
active
Object
Count = 0;
timerRefcount = 0;
itemDeactivated = false;
tickGeneration = 0;
function
initializePath(e) {
e.waypointX = new Array();
e.waypointY = new Array();
e.duration = new Array();
function
addWaypoint(e, number, x, y, duration) {
e.waypointX[number] = x;
e.waypointY[number] = y;
e.duration[number] = duration;
function
compact() {
var i, n, c;
n = new Array();
c = 0;
itemDeactivated = false;
for (i=0; i<active
Object
Count; i++) {
if (active
Object
s[i].active == true) {
n[c] = active
Object
s[i];
active
Object
s = n;
active
Object
Count = c;
function
tick(generation) {
if (generation < tickGeneration) {
// alert("Error "+generation);
return;
//alert("tick: "+generation);
if (itemDeactivated)
compact();
if (active
Object
Count == 0) {
return;
else {
for (i=0; i<active
Object
Count; i++) {
moveElement(active
Object
s[i]);
window.setTimeout("tick("+generation+");", tickDuration);
function
start(e) {
if (itemDeactivated)
compact();
active
Object
s[active
Object
Count] = e;
active
Object
Count++;
if (active
Object
Count == 1) {
tickGeneration++;
tick(tickGeneration);
function
runWaypoint(e, startPoint, endPoint) {
var startX, startY, endX, endY, duration;
if (e.waypointX == null)
return;
startX = e.waypointX[startPoint];
startY = e.waypointY[startPoint];
endX = e.waypointX[endPoint];
endY = e.waypointY[endPoint];
duration = e.duration[endPoint];
e.ticks = duration / tickDuration;
e.endPoint = endPoint;
e.active = true;
e.currTick = 0;
e.dx = (endX - startX) / e.ticks;
e.dy = (endY - startY) / e.ticks;
e.style.posLeft = startX;
e.style.posTop = startY;
start(e);
function
moveElement(e) {
e.style.posLeft += e.dx;
e.style.posTop += e.dy;
e.currTick++;
if (e.currTick > e.ticks) {
e.active = false;
itemDeactivated = true;
if (e.onpathcomplete != null) {
window.pathElement = e;
e.onpathcomplete()
To use this script in your
document
, do the following:
Load the script using the src
attribute
of the script element.
Initialize the paths using the initializePath
function
.
Set the way points using the addWaypoint
function
.
Set the path-complete handlers using the runWaypoint
function
.
The following sample
document
shows how this works.
SHOWExample
<div id=Item1 style="position: absolute; left: 0; top: 0;">Item1</div>
<div id=Item2 style="position: absolute; left: 0; top: 0;">Item2</div>
<div id=Item3 style="position: absolute; left: 0; top: 0;">Item3</div>
<div id=Item4 style="position: absolute; left: 0; top: 0;">Item4</div>
<div id=Item5 style="position: absolute; left: 0; top: 0;">Item5</div>
<div id=Item6 style="position: absolute; left: 0; top: 0;">Item6</div>
<input type=button value="Start" onclick="runWaypoint(Item1, 0, 1); runWaypoint(Item2, 0, 1);">
<div id=Debug>Generation</div>
<script src="htmlpath.js">
</script>
<script>
// need to call initializePath on all
object
s that will be moved with this mechanism
initializePath(Item1);
initializePath(Item2);
initializePath(Item3);
initializePath(Item4);
initializePath(Item5);
initializePath(Item6);
// the 0th waypoint is the initial position for waypoint #1
// syntax is item, waypoint, endx, endy, duration in msecs
addWaypoint(Item1, 0, 0, 0, 0);
addWaypoint(Item1, 1, 200, 200, 2000);
addWaypoint(Item2, 0, 100, 100, 0);
addWaypoint(Item2, 1, 400, 100, 4000);
addWaypoint(Item3, 0, 400, 400, 0);
addWaypoint(Item3, 1, 200, 100, 1000);
addWaypoint(Item4, 0, 0, 0, 0);
addWaypoint(Item4, 1, 200, 200, 2000);
addWaypoint(Item5, 0, 100, 100, 0);
addWaypoint(Item5, 1, 400, 100, 4000);
addWaypoint(Item6, 0, 400, 400, 0);
addWaypoint(Item6, 1, 200, 100, 1000);
function
end
function
() {
// syntax for runWaypoint is Item, start point, end point
runWaypoint(Item3, 0, 1);
runWaypoint(Item4, 0, 1);
runWaypoint(Item5, 0, 1);
runWaypoint(Item6, 0, 1);
function
end
function
2() {
runWaypoint(Item1, 0, 1);
Item1.onpathcomplete = end
function
;
Item6.onpathcomplete = end
function
2;
</script>
</body>
</html>
Show Me
演示 jQuery hide() 函数,隐藏所有 class="test" 的元素。
jQuery 语法
jQuery 语法是为 HTML 元素的选取编制的,可以对元素执行某些操作。
基础语法是:$(selector).action()
• 美元符号定义 jQuery
• 选择符(selector)“查询”和“查找” HTML 元素
• jQuery 的 action() 执行对元素的操作
$(this).hide() - 隐藏当前元素
$("p").hide() - 隐藏所有段落
$("p.test").hide() - 隐藏所有 class="test" 的段落
$("#test").hide() - 隐藏所有 id="test" 的元素
提示:jQuery 使用的语法是 XPath 与 CSS 选择器语法的组合。在本教程接下来的章节,您将学习到更多有关选择器的语法。
文档就绪函数
您也许已经注意到在我们的实例中的所有 jQuery 函数位于一个
document
ready 函数中:
$(
document
).ready(
function
(){
--- jQuery
function
s go here ----
这是为了防止文档在完全加载(就绪)之前运行 jQuery 代码。
二. jQuery 选择器
在前面的章节中,我们展示了一些有关如何选取 HTML 元素的实例。
关键点是学习 jQuery 选择器是如何准确地选取您希望应用效果的元素。
jQuery 元素选择器和属性选择器允许您通过标签名、属性名或内容对 HTML 元素进行选择。
选择器允许您对 HTML 元素组或单个元素进行操作。
在 HTML DOM 术语中:
选择器允许您对 DOM 元素组或单个 DOM 节点进行操作。
jQuery 元素选择器
jQuery 使用 CSS 选择器来选取 HTML 元素。
$("p") 选取 元素。
$("p.intro") 选取所有 class="intro" 的 元素。
$("p#demo") 选取 id="demo" 的第一个 元素。
jQuery 属性选择器
jQuery 使用 XPath 表达式来选择带有给定属性的元素。
$("[href]") 选取所有带有 href 属性的元素。
$("[href='#']") 选取所有带有 href 值等于 "#" 的元素。
$("[href!='#']") 选取所有带有 href 值不等于 "#" 的元素。
$("[href$='.jpg']") 选取所有 href 值以 ".jpg" 结尾的元素。
jQuery CSS 选择器
jQuery CSS 选择器可用于改变 HTML 元素的 CSS 属性。
下面的例子把所有 p 元素的背景颜色更改为红色:
$("p").css("background-color","red");
更多的选择器实例
语法 描述
$(this) 当前 HTML 元素
$("p") 所有 元素
$("p.intro") 所有 class="intro" 的 元素
$(".intro") 所有 class="intro" 的元素
$("#intro") id="intro" 的第一个元素
$("ul li:first") 每个 的第一个 元素
$("[href$='.jpg']") 所有带有以 ".jpg" 结尾的属性值的 href 属性
$("div#intro .head") id="intro" 的 元素中的所有 class="head" 的元素
三. jQuery 事件函数
jQuery 事件处理方法是 jQuery 中的核心函数。
事件处理程序指的是当 HTML 中发生某些事件时所调用的方法。术语由事件“触发”(或“激发”)经常会被使用。
通常会把 jQuery 代码放到 部分的事件处理方法中:
在上面的例子中,当按钮的点击事件被触发时会调用一个函数:
$("button").click(
function
() {..some code... } )
该方法隐藏所有 元素:
$("p").hide();
单独文件中的函数
如果您的网站包含许多页面,并且您希望您的 jQuery 函数易于维护,那么请把您的 jQuery 函数放到独立的 .js 文件中。
当我们在教程中演示 jQuery 时,会将函数直接添加到 部分中。不过,把它们放到一个单独的文件中会更好,就像这样(通过 src 属性来引用文件):
jQuery 使用 $ 符号作为 jQuery 的简介方式。
某些其他 JavaScript 库中的函数(比如 Prototype)同样使用 $ 符号。
jQuery 使用名为 noConflict() 的方法来
解决
该问题。
var jq=jQuery.noConflict(),帮助您使用自己的名称(比如 jq)来代替 $ 符号。
亲自试一试
由于 jQuery 是为处理 HTML 事件而特别设计的,那么当您遵循以下原则时,您的代码会更恰当且更易维护:
• 把所有 jQuery 代码置于事件处理函数中
• 把所有事件处理函数置于文档就绪事件处理器中
• 把 jQuery 代码置于单独的 .js 文件中
• 如果存在名称冲突,则重命名 jQuery 库
jQuery 事件
下面是 jQuery 中事件方法的一些例子:
Event 函数 绑定函数至
$(
document
).ready(
function
) 将函数绑定到文档的就绪事件(当文档完成加载时)
$(selector).click(
function
) 触发或将函数绑定到被选元素的点击事件
$(selector).dblclick(
function
) 触发或将函数绑定到被选元素的双击事件
$(selector).focus(
function
) 触发或将函数绑定到被选元素的获得焦点事件
$(selector).mouseover(
function
) 触发或将函数绑定到被选元素的鼠标悬停事件
四. jQuery实例
jQuery hide()
演示简单的 jQuery hide() 函数。
$(
document
).ready(
function
(){
$(".ex .hide").click(
function
(){
$(this).parents(".ex").hide("slow");
div.ex
background-color:#e5eecc;
padding:7px;
border:solid 1px #c3c3c3;
$(
document
).ready(
function
(){
$(".flip").click(
function
(){
$(".panel").slideToggle("slow");
div.panel,p.flip
margin:0px;
padding:5px;
text-align:center;
background:#e5eecc;
border:solid 1px #c3c3c3;
div.panel
height:120px;
;
$(
document
).ready(
function
(){
$("button").click(
function
(){
$("div").fadeTo("slow",0.25);
$(
document
).ready(
function
(){
$("#start").click(
function
(){
$("#box").animate({height:300},"slow");
$("#box").animate({width:300},"slow");
$("#box").animate({height:100},"slow");
$("#box").animate({width:100},"slow");
$(selector).show(speed,callback)
speed 参数规定显示或隐藏的速度。可以设置这些值:"slow", "fast", "normal" 或毫秒。
callback 参数是在 hide 或 show 函数完成之后被执行的函数名称。您将在本教程下面的章节学习更多有关 callback 参数的知识。
$("button").click(
function
(){
$("p").hide(1000);
亲自试一试
$(
document
).ready(
function
(){
$("button").click(
function
(){
$("p").hide(1000);
This is a paragraph with little content.
This is another small paragraph.
jQuery 切换
jQuery toggle() 函数使用 show() 或 hide() 函数来切换 HTML 元素的可见状态。
隐藏显示的元素,显示隐藏的元素。
$(selector).toggle(speed,callback)
speed 参数可以设置这些值:"slow", "fast", "normal" 或 毫秒。
$("button").click(
function
(){
$("p").toggle();
亲自试一试
$(
document
).ready(
function
(){
$("button").click(
function
(){
$("p").toggle();
This is a paragraph with little content.
This is another small paragraph.
callback 参数是在该函数完成之后被执行的函数名称。您将在本教程下面的章节学习更多有关 callback 参数的知识。
jQuery 滑动函数 - slideDown, slideUp, slideToggle
jQuery 拥有以下滑动函数:
$(selector).slideDown(speed,callback)
$(selector).slideUp(speed,callback)
$(selector).slideToggle(speed,callback)
speed 参数可以设置这些值:"slow", "fast", "normal" 或毫秒。
callback 参数是在该函数完成之后被执行的函数名称。您将在本教程下面的章节学习更多有关 callback 参数的知识。
slideDown() 实例
$(".flip").click(
function
(){
$(".panel").slideDown();
亲自试一试
$(
document
).ready(
function
(){
$(".flip").click(
function
(){
$(".panel").slideDown("slow");
div.panel,p.flip
margin:0px;
padding:5px;
text-align:center;
background:#e5eecc;
border:solid 1px #c3c3c3;
div.panel
height:120px;
;
$(
document
).ready(
function
(){
$(".flip").click(
function
(){
$(".panel").slideUp("slow");
div.panel,p.flip
margin:0px;
padding:5px;
text-align:center;
background:#e5eecc;
border:solid 1px #c3c3c3;
div.panel
height:120px;
$(
document
).ready(
function
(){
$(".flip").click(
function
(){
$(".panel").slideToggle("slow");
div.panel,p.flip
margin:0px;
padding:5px;
text-align:center;
background:#e5eecc;
border:solid 1px #c3c3c3;
div.panel
height:120px;
;
jQuery Fade 函数 - fadeIn(), fadeOut(), fadeTo()
jQuery 拥有以下 fade 函数:
$(selector).fadeIn(speed,callback)
$(selector).fadeOut(speed,callback)
$(selector).fadeTo(speed,opacity,callback)
speed 参数可以设置这些值:"slow", "fast", "normal" 或 毫秒。
fadeTo() 函数中的 opacity 参数规定减弱到给定的不透明度。
callback 参数是在该函数完成之后被执行的函数名称。您将在本教程下面的章节学习更多有关 callback 参数的知识。
fadeTo() 实例
$("button").click(
function
(){
$("div").fadeTo("slow",0.25);
亲自试一试
$(
document
).ready(
function
(){
$("button").click(
function
(){
$("div").fadeTo("slow",0.25);
jQuery 自定义动画
jQuery 函数创建自定义动画的语法:
$(selector).animate({params},[duration],[easing],[callback])
关键的参数是 params。它定义产生动画的 CSS 属性。可以同时设置多个此类属性:
animate({width:"70%",opacity:0.4,marginLeft:"0.6in",fontSize:"3em"});
第二个参数是 duration。它定义用来应用到动画的时间。它设置的值是:"slow", "fast", "normal" 或毫秒。
$(
document
).ready(
function
(){
$("#start").click(
function
(){
$("#box").animate({height:300},"slow");
$("#box").animate({width:300},"slow");
$("#box").animate({height:100},"slow");
$("#box").animate({width:100},"slow");
亲自试一试
$(
document
).ready(
function
(){
$("#start").click(
function
(){
$("#box").animate({height:300},"slow");
$("#box").animate({width:300},"slow");
$("#box").animate({height:100},"slow");
$("#box").animate({width:100},"slow");
$(
document
).ready(
function
(){
$("#start").click(
function
(){
$("#box").animate({left:"100px"},"slow");
$("#box").animate({fontSize:"3em"},"slow");
亲自试一试
$(
document
).ready(
function
(){
$("#start").click(
function
(){
$("#box").animate({left:"100px"},"slow");
$("#box").animate({fontSize:"3em"},"slow");
HTML 元素默认是静态定位,且无法移动。
如需使元素可以移动,请把 CSS 的 position 设置为 relative 或 absolute。
jQuery 效果 - 来自本页
函数 描述
$(selector).hide() 隐藏被选元素
$(selector).show() 显示被选元素
$(selector).toggle() 切换(在隐藏与显示之间)被选元素
$(selector).slideDown() 向下滑动(显示)被选元素
$(selector).slideUp() 向上滑动(隐藏)被选元素
$(selector).slideToggle() 对被选元素切换向上滑动和向下滑动
$(selector).fadeIn() 淡入被选元素
$(selector).fadeOut() 淡出被选元素
$(selector).fadeTo() 把被选元素淡出为给定的不透明度
$(selector).animate() 对被选元素执行自定义动画
五. JQuery Callback 函数
jQuery 动画的问题
许多 jQuery 函数涉及动画。这些函数也许会将 speed 或 duration 作为可选参数。
例子:$("p").hide("slow")
speed 或 duration 参数可以设置许多不同的值,比如 "slow", "fast", "normal" 或毫秒。
$("button").click(
function
(){
$("p").hide(1000);
亲自试一试
$(
document
).ready(
function
(){
$("button").click(
function
(){
$("p").hide(1000);
This is a paragraph with little content.
This is another small paragraph.
由于 JavaScript 语句(指令)是逐一执行的 - 按照次序,动画之后的语句可能会产生错误或页面冲突,因为动画还没有完成。
为了避免这个情况,您可以以参数的形式添加 Callback 函数。
jQuery Callback 函数
当动画 100% 完成后,即调用 Callback 函数。
典型的语法:
$(selector).hide(speed,callback)
callback 参数是一个在 hide 操作完成后被执行的函数。
错误(没有 callback)
$("p").hide(1000);
alert("The paragraph is now hidden");
亲自试一试
$(
document
).ready(
function
(){
$("button").click(
function
(){
$("p").hide(2000);
alert("The paragraph is now hidden");
This is a paragraph with little content.
正确(有 callback)
$("p").hide(1000,
function
(){
alert("The paragraph is now hidden");
亲自试一试
$(
document
).ready(
function
(){
$("button").click(
function
(){
$("p").hide(1000,
function
(){
alert("The paragraph is now hidden");
This is a paragraph with little content.
六. jQuery 包含很多供改变和操作 HTML 的强大函数。
改变 HTML 内容
$(selector).html(content)
html() 函数改变所匹配的 HTML 元素的内容(innerHTML)。
$("p").html("W3School");
亲自试一试
$(
document
).ready(
function
(){
$("button").click(
function
(){
$("p").html("W3School");
$(
document
).ready(
function
(){
$("button").click(
function
(){
$("p").append(" W3School.");
$(
document
).ready(
function
(){
$("button").click(
function
(){
$("p").after(" W3School.");
函数 描述
$(selector).html(content) 改变被选元素的(内部)HTML
$(selector).append(content) 向被选元素的(内部)HTML 追加内容
$(selector).prepend(content) 向被选元素的(内部)HTML “预置”(Prepend)内容
$(selector).after(content) 在被选元素之后添加 HTML
$(selector).before(content) 在被选元素之前添加 HTML
七. jQuery CSS 操作
jQuery 拥有三种用于 CSS 操作的重要函数:
• $(selector).css(name,value)
• $(selector).css({properties})
• $(selector).css(name)
CSS 操作实例
函数 css(name,value) 为所有匹配元素的给定 CSS 属性设置值:
$(selector).css(name,value)
$("p").css("background-color","red");
亲自试一试
$(
document
).ready(
function
(){
$("button").click(
function
(){
$("p").css("background-color","red");
$(selector).css({properties})
$("p").css({"background-color":"red","font-size":"200%"});
亲自试一试
$(
document
).ready(
function
(){
$("button").click(
function
(){
$("p").css({"background-color":"red","font-size":"200%"});
$(
document
).ready(
function
(){
$("div").click(
function
(){
$("#result").html($(this).css("background-color"));
$(
document
).ready(
function
(){
$("button").click(
function
(){
$("#id100").height("200px");
$(
document
).ready(
function
(){
$("button").click(
function
(){
$("#id200").width("300px");
CSS 属性 描述
$(selector).css(name,value) 为匹配元素设置样式属性的值
$(selector).css({properties}) 为匹配元素设置多个样式属性
$(selector).css(name) 获得第一个匹配元素的样式属性值
$(selector).height(value) 设置匹配元素的高度
$(selector).width(value) 设置匹配元素的宽度
八. jQuery 参考手册
@ jQuery 选择器
选择器 实例 选取
$("*") 所有元素
$("#lastname") id="lastname" 的元素
.class
$(".intro") 所有 class="intro" 的元素
element
$("p") 所有 元素
.class.class $(".intro.demo") 所有 class="intro" 且 class="demo" 的元素
:first
$("p:first") 第一个 元素
:last
$("p:last") 最后一个 元素
:even
$("tr:even") 所有偶数 元素
$("tr:odd") 所有奇数 元素
:eq(index)
$("ul li:eq(3)") 列表中的第四个元素(index 从 0 开始)
:gt(no)
$("ul li:gt(3)") 列出 index 大于 3 的元素
:lt(no)
$("ul li:lt(3)") 列出 index 小于 3 的元素
:not(selector) $("input:not(:empty)") 所有不为空的 input 元素
:header
$(":header") 所有标题元素 -
:animated
所有动画元素
:contains(text)
$(":contains('W3School')") 包含指定字符串的所有元素
:empty
$(":empty") 无子(元素)节点的所有元素
:hidden $("p:hidden") 所有隐藏的 元素
:visible
$("table:visible") 所有可见的表格
s1,s2,s3 $("th,td,.intro") 所有带有匹配选择的元素
[
attribute
]
$("[href]") 所有带有 href 属性的元素
[
attribute
=value]
$("[href='#']") 所有 href 属性的值等于 "#" 的元素
[
attribute
!=value]
$("[href!='#']") 所有 href 属性的值不等于 "#" 的元素
[
attribute
$=value]
$("[href$='.jpg']") 所有 href 属性的值包含 ".jpg" 的元素
:input
$(":input") 所有 元素
:text
$(":text") 所有 type="text" 的 元素
:password
$(":password") 所有 type="password" 的 元素
:radio
$(":radio") 所有 type="radio" 的 元素
:checkbox
$(":checkbox") 所有 type="checkbox" 的 元素
:submit
$(":submit") 所有 type="submit" 的 元素
:reset
$(":reset") 所有 type="reset" 的 元素
:button
$(":button") 所有 type="button" 的 元素
:image
$(":image") 所有 type="image" 的 元素
:file
$(":file") 所有 type="file" 的 元素
:enabled
$(":enabled") 所有激活的 input 元素
:disabled
$(":disabled") 所有禁用的 input 元素
:selected
$(":selected") 所有被选取的 input 元素
:checked
$(":checked") 所有被选中的 input 元素
@ jQuery 事件方法
事件方法会触发匹配元素的事件,或将函数绑定到所有匹配元素的某个事件。
触发实例:
$("button#demo").click()
上面的例子将触发 id="demo" 的 button 元素的 click 事件。
绑定实例:
$("button#demo").click(
function
(){$("img").hide()})
上面的例子会在点击 id="demo" 的按钮时隐藏所有图像。
方法 描述
bind()
向匹配元素附加一个或更多事件处理器
blur()
触发、或将函数绑定到指定元素的 blur 事件
change()
触发、或将函数绑定到指定元素的 change 事件
click()
触发、或将函数绑定到指定元素的 click 事件
dblclick()
触发、或将函数绑定到指定元素的 double click 事件
delegate()
向匹配元素的当前或未来的子元素附加一个或多个事件处理器
die()
移除所有通过 live() 函数添加的事件处理程序。
error()
触发、或将函数绑定到指定元素的 error 事件
event.isDefaultPrevented()
返回 event 对象上是否调用了 event.preventDefault()。
event.pageX
相对于文档左边缘的鼠标位置。
event.pageY
相对于文档上边缘的鼠标位置。
event.preventDefault()
阻止事件的默认动作。
event.result
包含由被指定事件触发的事件处理器返回的最后一个值。
event.target
触发事件的 DOM 元素。
event.timeStamp
该属性返回从 1970 年 1 月 1 日到事件发生时的毫秒数。
event.type
描述事件的类型。
event.which
指示按了哪个键或按钮。
focus()
触发、或将函数绑定到指定元素的 focus 事件
keydown()
触发、或将函数绑定到指定元素的 key down 事件
keypress()
触发、或将函数绑定到指定元素的 key press 事件
keyup()
触发、或将函数绑定到指定元素的 key up 事件
live()
触发、或将函数绑定到指定元素的 load 事件
load()
触发、或将函数绑定到指定元素的 load 事件
mousedown()
触发、或将函数绑定到指定元素的 mouse down 事件
mouseenter()
触发、或将函数绑定到指定元素的 mouse enter 事件
mouseleave()
触发、或将函数绑定到指定元素的 mouse leave 事件
mousemove()
触发、或将函数绑定到指定元素的 mouse move 事件
mouseout()
触发、或将函数绑定到指定元素的 mouse out 事件
mouseover()
触发、或将函数绑定到指定元素的 mouse over 事件
mouseup()
触发、或将函数绑定到指定元素的 mouse up 事件
one()
向匹配元素添加事件处理器。每个元素只能触发一次该处理器。
ready()
文档就绪事件(当 HTML 文档就绪可用时)
resize()
触发、或将函数绑定到指定元素的 resize 事件
scroll()
触发、或将函数绑定到指定元素的 scroll 事件
select()
触发、或将函数绑定到指定元素的 select 事件
submit()
触发、或将函数绑定到指定元素的 submit 事件
toggle()
绑定两个或多个事件处理器函数,当发生轮流的 click 事件时执行。
trigger()
所有匹配元素的指定事件
triggerHandler()
第一个被匹配元素的指定事件
unbind()
从匹配元素移除一个被添加的事件处理器
undelegate()
从匹配元素移除一个被添加的事件处理器,现在或将来
unload()
触发、或将函数绑定到指定元素的 unload 事件
@ jQuery 效果函数
方法 描述
animate()
对被选元素应用“自定义”的动画
clearQueue()
对被选元素移除所有排队的函数(仍未运行的)
delay() 对被选元素的所有排队函数(仍未运行)设置延迟
dequeue() 运行被选元素的下一个排队函数
fadeIn()
淡入被选元素至完全不透明
fadeOut()
淡出被选元素至完全不透明
fadeTo()
把被选元素减弱至给定的不透明度
hide()
隐藏被选的元素
queue() 显示被选元素的排队函数
show()
显示被选的元素
slideDown()
通过调整高度来滑动显示被选元素
slideToggle()
对被选元素进行滑动隐藏和滑动显示的切换
slideUp()
通过调整高度来滑动隐藏被选元素
stop()
停止在被选元素上运行动画
toggle()
对被选元素进行隐藏和显示的切换
@ jQuery 文档操作方法
这些方法对于 XML 文档和 HTML 文档均是适用的,除了:html()。
方法 描述
addClass()
向匹配的元素添加指定的类名。
after()
在匹配的元素之后插入内容。
append()
向匹配的元素内部追加内容。
appendTo()
向匹配的元素内部追加内容。
attr()
设置或返回匹配元素的属性和值。
before()
在每个匹配的元素之前插入内容。
clone()
创建匹配元素集合的副本。
detach()
从 DOM 中移除匹配元素集合。
empty()
删除匹配的元素集合中所有的子节点。
hasClass()
检查匹配的元素是否拥有指定的类。
html()
设置或返回匹配的元素集合中的 HTML 内容。
insertAfter()
把匹配的元素插入到另一个指定的元素集合的后面。
insertBefore()
把匹配的元素插入到另一个指定的元素集合的前面。
prepend()
向每个匹配的元素内部前置内容。
prependTo()
向每个匹配的元素内部前置内容。
remove()
移除所有匹配的元素。
removeAttr()
从所有匹配的元素中移除指定的属性。
removeClass()
从所有匹配的元素中删除全部或者指定的类。
replaceAll()
用匹配的元素替换所有匹配到的元素。
replaceWith()
用新内容替换匹配的元素。
text()
设置或返回匹配元素的内容。
toggleClass()
从匹配的元素中添加或删除一个类。
unwrap()
移除并替换指定元素的父元素。
val()
设置或返回匹配元素的值。
wrap()
把匹配的元素用指定的内容或元素包裹起来。
wrapAll()
把所有匹配的元素用指定的内容或元素包裹起来。
wrapinner()
将每一个匹配的元素的子内容用指定的内容或元素包裹起来。
@ jQuery 属性操作方法
下面列出的这些方法获得或设置元素的 DOM 属性。
这些方法对于 XML 文档和 HTML 文档均是适用的,除了:html()。
方法 描述
addClass()
向匹配的元素添加指定的类名。
attr()
设置或返回匹配元素的属性和值。
hasClass()
检查匹配的元素是否拥有指定的类。
html()
设置或返回匹配的元素集合中的 HTML 内容。
removeAttr()
从所有匹配的元素中移除指定的属性。
removeClass()
从所有匹配的元素中删除全部或者指定的类。
toggleClass()
从匹配的元素中添加或删除一个类。
val()
设置或返回匹配元素的值。
@ jQuery CSS 操作函数
下面列出的这些方法设置或返回元素的 CSS 相关属性。
CSS 属性 描述
css()
设置或返回匹配元素的样式属性。
height()
设置或返回匹配元素的高度。
offset()
返回第一个匹配元素相对于文档的位置。
offsetParent()
返回最近的定位祖先元素。
position()
返回第一个匹配元素相对于父元素的位置。
scrollLeft()
设置或返回匹配元素相对滚动条顶部的偏移。
scrollTop()
设置或返回匹配元素相对滚动条左侧的偏移。
width()
设置或返回匹配元素的宽度。
@jQuery Ajax 操作函数
jQuery 库拥有完整的 Ajax 兼容套件。其中的函数和方法允许我们在不刷新浏览器的情况下从服务器加载数据。
函数 描述
jQuery.ajax()
执行异步 HTTP (Ajax) 请求。
.ajaxComplete()
当 Ajax 请求完成时注册要调用的处理程序。这是一个 Ajax 事件。
.ajaxError()
当 Ajax 请求完成且出现错误时注册要调用的处理程序。这是一个 Ajax 事件。
.ajaxSend()
在 Ajax 请求发送之前显示一条消息。
jQuery.ajaxSetup()
设置将来的 Ajax 请求的默认值。
.ajaxStart()
当首个 Ajax 请求完成开始时注册要调用的处理程序。这是一个 Ajax 事件。
.ajaxStop()
当所有 Ajax 请求完成时注册要调用的处理程序。这是一个 Ajax 事件。
.ajaxSuccess()
当 Ajax 请求成功完成时显示一条消息。
jQuery.get()
使用 HTTP GET 请求从服务器加载数据。
jQuery.getJSON()
使用 HTTP GET 请求从服务器加载 JSON 编码数据。
jQuery.getScript()
使用 HTTP GET 请求从服务器加载 JavaScript 文件,然后执行该文件。
.load()
从服务器加载数据,然后把返回到 HTML 放入匹配元素。
jQuery.param()
创建数组或对象的序列化表示,适合在 URL 查询字符串或 Ajax 请求中使用。
jQuery.post()
使用 HTTP POST 请求从服务器加载数据。
.serialize()
将表单内容序列化为字符串。
.serializeArray()
序列化表单元素,返回 JSON 数据结构数据。
@ jQuery 遍历函数
jQuery 遍历函数包括了用于筛选、查找和串联元素的方法。
函数 描述
.add() 将元素添加到匹配元素的集合中。
.andSelf() 把堆栈中之前的元素集添加到当前集合中。
.children() 获得匹配元素集合中每个元素的所有子元素。
.closest() 从元素本身开始,逐级向上级元素匹配,并返回最先匹配的祖先元素。
.contents() 获得匹配元素集合中每个元素的子元素,包括文本和注释节点。
.each() 对 jQuery 对象进行迭代,为每个匹配元素执行函数。
.end() 结束当前链中最近的一次筛选操作,并将匹配元素集合返回到前一次的状态。
.eq() 将匹配元素集合缩减为位于指定索引的新元素。
.filter() 将匹配元素集合缩减为匹配选择器或匹配函数返回值的新元素。
.find() 获得当前匹配元素集合中每个元素的后代,由选择器进行筛选。
.first() 将匹配元素集合缩减为集合中的第一个元素。
.has() 将匹配元素集合缩减为包含特定元素的后代的集合。
.is() 根据选择器检查当前的匹配元素集合,如果存在至少一个匹配元素,则返回 true。
.last() 将匹配元素集合缩减为集合中的最后一个元素。
.map() 把当前匹配集合中的每个元素传递给一个函数,产生包含返回值的新的 jQuery 对象。
.next() 获得匹配元素集合中每个元素紧邻的同辈元素。
.nextAll() 获得匹配元素集合中每个元素之后的所有同辈元素,由选择器进行筛选(可选)。
.nextUntil() 获得每个元素之后所有的同辈元素,直到遇到匹配选择器的元素为止。
.not() 从匹配元素集合中删除元素。
.offsetParent() 获得用于定位的第一个父元素。
.parent() 获得当前匹配元素集合中每个元素的父元素,由选择器筛选(可选)。
.parentsUntil() 获得当前匹配元素集合中每个元素的祖先元素,直到遇到匹配选择器的元素为止。
.prev() 获得匹配元素集合中每个元素紧邻的前一个同辈元素,由选择器筛选(可选)。
.prevAll() 获得匹配元素集合中每个元素之前的所有同辈元素,由选择器进行筛选(可选)。
.prevUntil() 获得每个元素之前所有的同辈元素,直到遇到匹配选择器的元素为止。
.siblings() 获得匹配元素集合中所有元素的同辈元素,由选择器筛选(可选)。
.slice() 将匹配元素集合缩减为指定范围的子集。
@ jQuery 数据存储函数
这些方法允许我们将指定的 DOM 元素与任意数据相关联。
函数 描述
.data() 存储与匹配元素相关的任意数据。
jQuery.data() 存储与指定元素相关的任意数据。
.removeData() 移除之前存放的数据。
jQuery.removeData() 移除之前存放的数据。
@ jQuery 队列控制函数
函数 描述
.clearQueue() 从队列中删除所有未运行的项目。
.dequeue() 从队列最前端移除一个队列函数,并执行它。
jQuery.dequeue() 从队列最前端移除一个队列函数,并执行它。
.queue() 显示或操作匹配元素所执行函数的队列。
jQuery.queue() 显示或操作匹配元素所执行函数的队列。
九. jQuery 核心函数
函数 描述
jQuery()
接受一个字符串,其中包含了用于匹配元素集合的 CSS 选择器。
找出所有属于 div 元素的子元素的 p 元素,然后设置其边框属性:
$("div > p").css("border", "1px solid gray");
亲自试一试
定义和用法
jQuery() 方法接受一个字符串,其中包含了用于匹配元素集合的 CSS 选择器。
jQuery() 函数有三种语法:
接受一个字符串,其中包含了用于匹配元素集合的 CSS 选择器:
jQuery(selector, [context])
使用原始 HTML 的字符串来创建 DOM 元素:
jQuery(html,[owner
Document
])
绑定一个在 DOM 文档载入完成后执行的函数:
jQuery( callback )
jQuery( selector, [ context ] )
该语法有以下几种用法:
用法 1 :设置选择器环境
jQuery(selector, [context])
默认情况下,选择器从文档根部对 DOM 进行搜索。不过,可以为 $() 设置可选的 context 参数。
例如,如果我们希望在一个 callback 中搜索一个元素,可以限定下面的搜索:
$("div.foo").click(
function
() {
$("span", this).addClass("bar");
由于我们已经将 span 选择器限定到 this 这个环境中,只有被点击元素中的 span 会得到附加的 class。
在内部,选择器环境是通过 .find() 方法实现的,因此 $("span", this) 等价于 $(this).find("span")。
jQuery 的核心功能都是通过这个函数实现的。jQuery中的一切都基于这个函数,或者说都是在以某种方式使用这个函数。这个函数最基本的用法就是向它传递一个表达式(通常由 CSS 选择器组成),然后根据这个表达式来查找所有匹配的元素。
默认情况下, 如果没有指定 context 参数,$() 将在当前的 HTML
document
中查找 DOM 元素;如果指定了 context 参数,如一个 DOM 元素集或 jQuery 对象,那就会在这个 context 中查找。在 jQuery 1.3.2 以后,其返回的元素顺序等同于在 context 中出现的先后顺序。
用法 2 :使用 DOM 元素
jQuery(element)
该函数允许我们通过使用以其他方式找到的 DOM 元素来创建 jQuery 对象。该功能通常的用法是,对已经通过 this 关键字传递到 callback 函数的元素调用 jQuery 的方法:
$("div.foo").click(
function
() {
$(this).slideUp();
此例会在元素被点击时使用滑动动画对其进行隐藏。由于处理程序接受的 this 关键词中的被点击项目是纯的 DOM 元素,因此在对其调用 jQuery 的方法之前,必须用 jQuery 对象包装该元素。
这个函数也可以接收 XML 文档和 Window 对象(虽然它们不是 DOM 元素)作为有效的参数。
当 XML 数据从 Ajax 调用中返回后,我们可以使用 $() 函数通过 jQuery 对象包装该数据。一旦完成,我们就可以使用 .find() 和其他 DOM 遍历方法来取回 XML 结构中单个元素。
用法 3 :克隆 jQuery 对象
jQuery(jQuery
object
)
当以参数的形式向 $() 函数传递 jQuery 对象后,会创建一个该对象的副本。与初始对象一样,新的 jQuery 对象引用相同的 DOM 元素。
用法 4 :返回空的集合
jQuery()
对于 jQuery 1.4,调用无参数的 jQuery() 方法会返回空的 jQuery 集合。在之前版本的 jQuery 中,这样会返回包含
document
节点的集合。
jQuery( html, [ owner
Document
] )
该语法有以下几种用法:
用法 1 :创建新的元素
jQuery(html,[owner
Document
])
你可以传递一个手写的 HTML 字符串,或者由某些模板引擎或插件创建的字符串,也可以是通过 AJAX 加载过来的字符串。但是在你创建 input 元素的时会有限制,可以参考第二个示例。
当然这个字符串可以包含斜杠 (比如一个图像地址),还有反斜杠。当你创建单个元素时,请使用闭合标签或 XHTML 格式。例如,创建一个 span ,可以用 $("") 或 $("") ,但不推荐 $("")。在 jQuery 中,这个语法等同于 $(
document
.createElement("span"))。
如果以参数的形式将字符串传递给 $(),jQuery 会检查字符串是否是 HTML (比如,字符串某些位置存在标签)。如果不是,则把字符串解释为选择器表达式,请见上面的讲解。但如果字符串是 HTML 片段,则 jQuery 试图创建由该 HTML 片段描述的 DOM 元素。然后会创建并返回一个引用这些 DOM 元素的 jQuery 对象:
$("My new text").appendTo("body");
如果 HTML 片段比不含属性的简单标签更复杂,如同上面例子中的 HTML,那么元素实际的创建过程是由浏览器的 innerHTML 机制完成的。具体地讲,jQuery 会创建新的 元素,然后为传入的 HTML 片段设置元素的 innerHTML 属性。当参数只是简单的标签,比如$("") 或 $(""),jQuery 会通过内生的 JavaScript createElement() 函数来创建元素。
要确保跨平台兼容性,片段的结构必须良好。能够包含其他元素的标签必须成对出现(带有关闭标签):
$("");
不过,jQuery 也允许类似 XML 的标签语法:
$("");
无法包含其他元素的标签可以关闭,也可以不关闭:
$("");
$("");
用法 2 :设置属性和事件
jQuery(html,props)
对于 jQuery 1.4,我们可以向第二个参数传递一个属性映射。该参数接受能够传递给 .attr() 方法的属性的超集。此外,可以传递任意的事件类型,并可以调用下面的 jQuery 方法:val, css, html, text, data, width, height, or offset.
注意,Internet Explorer 不允许你创建 input 元素并改变其类型;您必须使用例如 "" 来规定类型。
创建一个 元素,同时设定 type 属性、属性值,以及一些事件。
$("", {
type: "text",
val: "Test",
focusin:
function
() {
$(this).addClass("active");
focusout:
function
() {
$(this).removeClass("active");
}).appendTo("form");
jQuery( callback )
允许你绑定一个在 DOM 文档载入完成后执行的函数。
该函数的作用如同 $(
document
).ready() 一样,只不过用这个函数时,需要把页面中所有需要在 DOM 加载完成时执行的其他 $() 操作符都包装到其中来。尽管从技术上来说,这个函数是可链接的,但真正以这种方式链接的情况并不多。
当DOM加载完成后,执行其中的函数:
$(
function
(){
// 文档就绪
函数 描述
jQuery.noConflict()
运行这个函数将变量 $ 的控制权让渡给第一个实现它的那个库。
定义和用法
noConflict() 方法让渡变量 $ 的 jQuery 控制权。
jQuery.noConflict([removeAll])
参数 描述
removeAll 布尔值。指示是否允许彻底将 jQuery 变量还原。
许多 JavaScript 库使用 $ 作为函数或变量名,jQuery 也一样。在 jQuery 中,$ 仅仅是 jQuery 的别名,因此即使不使用 $ 也能保证所有功能性。假如我们需要使用 jQuery 之外的另一 JavaScript 库,我们可以通过调用 $.noConflict() 向该库返回控制权:
此外,通过向该方法传递参数 true,我们可以将 $ 和 jQuery 的控制权都交还给原来的库。用之前请考虑清楚!
这是相对于简单的 noConflict 方法更极端的版本,因为这将完全重新定义 jQuery。这通常用于一种极端的情况,比如你想要将 jQuery 嵌入一个高度冲突的环境。注意:调用此方法后极有可能导致插件失效。
将 $ 引用的对象映射回原始的对象:
jQuery.noConflict();
jQuery("div p").hide(); // 使用 jQuery
$("content").style.display = "none"; // 使用其他库的 $()
恢复使用别名 $,然后创建并执行一个函数,在这个函数的作用域中仍然将 $ 作为 jQuery 的别名来使用。在这个函数中,原来的 $ 对象是无效的。这个函数对于大多数不依赖于其他库的插件都十分有效:
jQuery.noConflict();
(
function
($) {
$(
function
() {
// 使用 $ 作为 jQuery 别名的代码
})(jQuery);
... // 其他用 $ 作为别名的库的代码
可以将 jQuery.noConflict() 与简写的 ready 结合,使代码更紧凑:
jQuery.noConflict()(
function
(){
// 使用 jQuery 的代码
... // 其他库使用 $ 做别名的代码
创建一个新的别名用以在接下来的库中使用 jQuery 对象:
var j = jQuery.noConflict();
j("div p").hide(); // 基于 jQuery 的代码
$("content").style.display = "none"; // 基于其他库的 $() 代码
完全将 jQuery 移到一个新的命名空间:
var dom = {};
dom.query = jQuery.noConflict(true);
dom.query("div p").hide(); // 新 jQuery 的代码
$("content").style.display = "none"; // 另一个库 $() 的代码
jQuery("div > p").hide(); // 另一个版本 jQuery 的代码
*******************************************
************ WPTOOLS 6 History ************
********** http://www.wpcubed.com *********
**** WPCubed GmbH, Munich, Germany ********
*******************************************
* - = bug fix
* + = new feature
* * = changed, expanded feature
*******************************************
* IMPORTANT: Please edit file WPINC.INC to
* activate WPREPORTER, WPSPELL, wPDF, TBX
* and GraphicEx / PNGImage support!
* Delphi 2009 and later as inbuilt PNGImage
*******************************************
* THE MANUAL (PDF) AND REFERENCE (HLP,CHM)
* ARE PROVIDED AS SEPERATE DOWNLOADS!
*******************************************
~~~~~~~~~~ Important Notes ~~~~~~~~~~~~
- With Delphi XE2 or XE3 it is possible to compile WPTools into a 64 bit applications.
You need WPTools 6 PRO or WPTools 6 Premium for this.
- Localization and Inch/CM selection see
http://www.wpcubed.com/manuals/wp5man/index.html?localization.htm
Also see demo\tasks\localization. There are the required XML files.
- if flag wpDontAddExternalFontLeading is active in property FormatOptionsEx
the text will be formatted more like WPTools4/MS-Word. You can alternatively
set global variable WPDoNotAddExternalFontLeading := TRUE and
select the Printer WYSIWYG mode: TWPRichText1.HeaderFooter.UpdateReformatMode(true)
- The FormatOption wpDisableAutosizeTables can be required for good
display of tables. wpNoMinimumCellPadding for narrow layout, too.
- If you plan to use DBWPRichText please check
http://www.wpcubed.com/forum/viewtopic.php?p=3456
- The reader/writer receive their options through "FormatStrings"
please see list at http://www.wpcubed.com/manuals/formatstrings.htm
- WPTools is configured using the file WPINC.INC,
here WPREPORTER is activated and the optional WPShared, WPSPell and wPDF
can be selected to be compiled into main WPTools package.
- also see FAQ: http://www.wpcubed.com/forum/viewforum.php?f=15
- and support forum: http://www.wpcubed.com/forum/
- WPTools 6 PRO and PREMIUM will be compiled as Version 5 if $define "WP6" is not set
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
We recommend to use TMS components to create modern user interfaces, toolbars and ribbons.
19.2.2013 - WPTools 6.29.1
- fix in rtf writing code to solve problem with merged cells
- fix possible rangecheck error
- fix problem with Text
Object
.LoadFromFile and Delphi XE3
* RTF reader now handles UNC file links which use "\\" in the path
* the cursor was not painted if DoubleBuffered was set to true for the parent of the editor
+ WPTools Premium: Saves and loads \column
* improved theming of TWPToolbar and TWPToolPanel
+ new event: OnPaintDesktopBackground. It can be used to draw the parent of the editor, for example
if it is a TMS panel or pager control.
Example:
procedure TForm1.WPRichText1PaintDesktopBackground(Sender: T
Object
; Canvas: TCanvas; R: TRect);
begin
// This would paint the TWPRichText, too - but TWPRichText is locked for repaint during this event
AdvOfficePager1.PaintTo(Canvas, -WPRichText1.Left, -WPRichText1.Top);
- HighlightTextColor can now also be used if 2Pass Painting is used
21.12.2012 - WPTools 6.29
- images in RTF label were not painted when label was moved
+ added support for XE3 to WPTools STD edition
* stream RTFvariables were not loaded from WPT format. They are loaded now.
9.11.2012 - WPTools 6.28
- Update to RTF reader to load landscape flag for sections better
- when page mirror was used, after a page break the text indentation was sometimes wrong
- hyphenation code was broken
- workaround for word files which have space characters in table definitions
16.10.2012 - WPTools 6.27''
* some additions to the PRO edition for XE3
26.9.2012 - WPTools 6.27'
* The PRO Version now supports Delphi XE3
3.8.2012 - WPTools 6.27
- fix for wrong display of tables with header and footer rows. Sometimes both were painted without any data.
+ to load old Hiedit templates as RTF code use the formatstring -HiEditFields. This will create merge fields for ALL fields.
- NL sign was not shown right after CTRL+ENTER was pressed (requires ShowNL)
- fix for rangecheck exception with paintpages array
- fix for footer and page mirror
- doubleclick word selection now stops at NL
- Workaround for Windows Spooler problems - some images would get lost
- sections use footer and header of previous section, not general
- ASetBorder did change all border types
12.3.2012 - WPTools 6.25.4
* allows changing of column width in redonly editors. Can be switchoed off in EditOptions or set
compiler define TOTALREADONLY
+ wpDisableSelectAll in EditOptionsEx2
* changed reformat/repaint after Undo/Redo
- pro and premium: Due to a problem with precompiler cursoir selection did not work correctly
8.3.2012 - WPTools 6.25.3'
- borders for paragraphs with multiple lines were not drawn correctly
- change in DBWPRich.pas to use LoadFromString instead of Text
- fix possible range check error
- change in WPTbar.pas to use different default for BevelOuter
- change in WPIOHTML to use default charattr of paragraph is a paragraph is empty
9.2.2012 - WPTools 6.25.2
* new 2-pass painting triggers CustomPaint event only on second loop (when the text is painted)
* changed protection of empty paragraph to WPTools 5 way
+ inside of the OnPrepareImageForSaving event it is now possible to set Writer.CurrentImageName
to the name of the file which should be saved. This is only useful if ObjRef = nil and so no ObjRef.Filename can be set.
+ TParagraph.GetSubText now has optional parameter to disable the
object
reference char codes #1, #2 and #3
* HTML writer ignores #13 codes when writing the text.
* additional savety code in HTML writer
* HTML reader sets image width and height to contents, (if possible)
* HTML reader: changes UTF8 handling for UTF8Sequence=1
* HTML reader does not stop on \0 anymore
* HTML writer writes img tag also for empty images IF a name is provided in event PrepareImageforSaving
* HTML reader does not add space at end anymore
- the UNICODE reader uses
attribute
of current paragraph. This is important for consistent
behaviour between ANSI and UNICODE version
- when writing HTML background color is not set to white when shading is 0. 0 is treated as default value.
- image align center and right now works in HTML
- fix an endless loop when image was too large
- improvement of table border drawing
- improvement for right align in table cells
+ numbering will be used when wpFormatAsWebpage was set in AsWebpage
10.1.2012 - WPTools 6.25
+ HTML reader reads cell heighs
- RTF writer writes background color easier to understand by Word
* improved XML reader/writer (unit WPIOXml1)
* improved word wise cursor movement when fields are used
+ new "paint
attribute
s" mode.
Use WPRichText1.BrushStart to select this mode.
- dashes were not painted using the current font color
- some stability improvements
7.11.2011 - WPTools 6.22
+ procedure TParagraph.CellSelectionAdd;
+ procedure CellSelectionRemove;
+ EditOptionsEx2: wpCellMultiSelect - allows multiselection in tables when CTRL is pressed
+ improved XML import/export (unit WPIOXML1.PAS)
- some smaller bugs fixed
3.11.2011 - WPTools 6.21.2
- fix problem with TWPToolButton
- improved HTML writer to write parameters in ""
- improved display of arabic text
24.10.2011 - WPTools 6.21.1
- fix problem when painting insertpoints after tab stops. They were painted two times.
- fix in XML and HTML writer. Close tags when extracting text from fields
19.10.2011 - WPTools 6.21
+ CodeLocate can now also accessed in non-visual TWPRTFDataCollection and not just TWPCustomRtfEdit
+ CodeSelect can now also accessed in non-visual TWPRTFDataCollection and not just TWPCustomRtfEdit
+ TWPRTFDataCollection.CodeLocatePair( FormatName : String; var spar, epar : TParagraph; var spos, epos, : Integer ) :Integer;
- solves problem with integrated Bin64 decoder
+ DeleteField now has optional "Contents" parameter to delete a certain field with contents
+ the text writer now understand the option -softlinebreaks to create a \n at the end of
every line. In fact all soft line breaks will be handled like the #10 code.
+ CodeLoadSaveEmbeddedText - load or save text in fields, bookmarks, hyperlinks
+ the regular save and load methods (LoadFromFile, SaveToFile) can now access text wrapped by paired
object
s.
specify the fieldname in the format string, i.e. "f:name=RTF" to save or load the contents of the field "name".
+ There is an overloaded LoadFromString which expects a WideString as parameter (Delphi 6+)
- The Setup named the Delphi 2009 files "Delphi 2005" due to a typo. (Delphi 2005 units are not included anymore)
- fix probable range check error in WPRTEDEFS
13.10.2011 - WPTools 6.20
+ completely new setup procedure. The PRO and Premium releases don't include
object
files which makes them much smaller.
* compiled new WPTools 6 Reference (CHM file)
* Delphi XE2: several small changes to improve theming support
* Delphi XE2: several small changes to provide compatibilty to 64bit compiler (requires WPTools PRO)
+ new demo developed with Delphi XE2, showcases actions, splitscreen, simulated MDI and property dialogs (Demos\XE2\WPWord)
+ wpDeleteAtEOFRemovesSpaceAfter in EditOptionsEx2
- Text
Object
sGetList did not work
* WPRuler uses Delphi XE2 VCL Theme plus
* added defaults to properties TWPRichText
- change in RTF reader to let section inherit the default layout, not the current page layout
- fix of problem with table borders when also PageMirror was used.
* change to DeleteMarkedChar. It now has additional parameter DeleteEmptyParAndTables : Boolean
* change in unit WPWordConv to handle RTF as DOC files if they do not start with "{\rtf"
* updated border dialog TWPParagraphBorderDlgEx
* updated border drawing code - now supports dotted lines with wider lines.
* modified method DeleteColumn
* modified WPT reading code to repair table width which were negative
+ improved image rendering code for transparent (PNG) images. They will be drawn transparently
also when scaled and also in high resolution rendering mode.
+ new code to draw dotted lines which also supports wider lines
+ new
function
WPReplaceTokens (unit WPUtils.PAS). It is a ReplaceTokens
function
to be used
on a TWPRTFDataCollection, not TWPRichText
- WPPremium - fix problem when there were too columns
* MergeText now restores before Merge Cursor position and selection (except for cell selection)
* resizing a table column does not move the cursor to the nearby cell anymore
* different frame line when resizing columns and rows
+ InsertColumn now also works if wpAllowSplitOfCombinedCellsOnly was used in EditOptionsEx
+ new event OnPaintTextFrameRect let you paint background and borders for text frames,
i.e. the text body or, with WPTools Premium, custom frames.
+ WPPREMIUM: In OnMeasureTextPage it is possible to set columns for certain pages.
Using PageInfo.colflags=1 it is possible to activate a line between the columns
It is also possible to add custom frames using PageInfo.rtfpage.AddFrameRect.
+ new ViewOptionEx: wpHideParBordersBeforAndAfterPageBreaks
+ improved paint routine now avoids clipping of characters which were overlapping their bounding box,
such as italic letters or "f".
The improvement is especially visible when selecting text or using character background colors
+ WPPREMIUM: it is now possible to print a line between columns using wpShowColumnCenterLine in ViewOptionsEx
+ With WPTools Premium it is now possible to print a line between certain colums -
use par.ASet(WPAT_COLFLAGS, WPCOLUM_LINE);
+ paragraph styles can now contain border definion for paragraphs
+ TWPTextObjList now has a IndexOfSource
function
and a Source[] string array to access the
object
s
* revised code to draw double borders - always draws twou lines on screen even when zoomed
* improved saving of numbering
attribute
s with styles
* style dialog can now apply number level even if style does not have numbering yet.
* revised wpNoEditOutsideTable - was not checked for keyboard input
* fix problem with - - - - - at end of line
- fix problem with spell-as-you go after hyperlinks
- fix problem with page numbers in sections when tables were spanning pages
- fix problem with right aligned negative numbers in merge fields
* automatic text
attribute
was not inherited to tables inserted in fields
* images with mode "under text " can now be also clicked at positiones where there is no text.
- WPLngCtr now defines DONT_OVERRIDE_COM, that fixes the IDE problem with DevExpress Toolbar controls
18.7.2011 - WPTools 6.16.2
*
Object
Mode wpobjPositionAtCenter can be used to aligh character based images to the center line. (Other RTF reader will not understand this feature)
* some changes to prepare 64bit compatibility (requires WPTools 6 PRO)
7.7.2011 - WPTools 6.16.1
* change to avoid flickering when doing auto scroll
* message strings are now loaded from resource strings
- modified DBCS support for RTF reader
* outerborder action now works for sinle cells and paragraphs
15.6.2011 - WPTools 6.16
+ Many improvements to new Border Dialog.
It is now possible to apply borders and colors to odd and even rows only.
* RTF reader defines symbol IGNORE_AUTOWIDTH_TBL. This disable the Word2010 auto width columns which are not read
correctly. Reason: Word 2010 seem to always add an empty column to the end.
+ using WPRichText1.Caret.Blink:=true it is possible to activate the blinking caret (0.5sec interval).
* updated actions to apply inner and outer borders to selected cells
* update to ExSymbolDialog, Tab and Table dialog
- improved WPTOOLS reader to read and apply outline properties to current outline setting (unless wpLoadDoesNotOverride is used)
- wpsec_ResetPageNumber flag is now saved in WPTOOLS format
* tripple click in margin selects paragraph
+ double click in margin selects current cell
+ tripple click in margin selects current row
- change for PaintEngine and TWPRichTextLabel to not shrink tables which are small enough to fit the page
This solves a problem with dissapearing text in WPRichTextLabel
- fix selection problem when several images were linked to same paragraph
- when moving images the Z order will not be reset
- HTML Writer: A style with name "DIV" will be added to the style sheet to save the default font
- HTML Writer: BaseFont tag will now be written with font size (requires -writebasefont option)
- improved display of character background color for fields and other special code
- Saving a style sheet did not work with Unicode Compiler
8.5.2011 - WPTools 6.15
* updated border painting
* updated Inner/Outer Border Action
* new
object
sizing routine lets the user make the size larger than the page
- update in WPTools reader to overwrite outline styles when loading tzhe same group
9.3.2011 - WPTools 6.14.6
* change in format routine to fix problem when a nested table cell caused a page break.
14.12.2010 - WPTools 6.14
* fix for SetAsString code (Unicode Delphi)
* several fixes and updates in editor
* WPTools Premium: $define DONT_AUTOENTER_TEXTBOXES in WPINC.INC to switch of the behavior,
that when editing a textbox the user can click on any other and edit that.
22.9.2010 - WPTools 6.13.3
* WPCtrMemo.PAS now defines TEXT_IS_UNICODE fro Delphi 2009 and later. Now the property Text and SelText reads and writes unicode strigs
* change in RTF reader to read ANSI characters in the range 128..255
* Tables and rows can now be hidden. (TParagraph.Hidden)
* The Lines property now supports unicode strings (Delphi 2009 and later)
+ HTML reader and writer now use the entitly as soft hyphen
27.8.2010 - WPTools 6.13.2a
+ new ViewOptionEx wpUnderlineWebLinks. If active links like http://www.wptools.de will be drawn using the
attribute
s for hyperlinks
The HyperlinkCursor will be selected and the hyperlink event will be triggered
* other fixes in RTF engine
- Memo._OverrideCharset was not set to -1
27.7.2010 - WPTools 6.13.1
+ WPRichText1.Memo.ColorGridLines can be used to change the color of the grid lines (ViewOptions)
23.7.2010 - WPTools 6.13
* several improvements of editor
* improvement to RTF writer to when writing table cells
* improved right aligned text
* fixed problem with line heights of lines which are empty except for new line character
22.6.2010 - WPTools 6.12.1b
- fix for problem when pressing Accent + Backspace
- fix for WPReporter to assign default
attribute
to destination
- fix for WPReporter to move images when converting templates
18.6.2010 - WPTools 6.12.1a
* the new border dialog now reads the current border
attribute
s from table cells, tables or selections
- Premium: fix problem when loading columns which started on first line
- fix in wpfUseKerning mode (FormatOptionsEx2) - it did not work as expected with some texts
(We recommend to use wpfUseKerning - it produces better print quality on screen)
* Pasting of HTML now works better
- fix for right tabs when also borders were used
13.6.2010 - WPTools 6.12
+ all new, powerful yet intuitive Border dialog.
The border dialog can modify a range of selected cells, columns, rows, tables and also only
modify certain properties while leaving the others unchanged.
You need to activate the compiler symbol NEWBORDER to use it by default
+ new method: procedure SetBorderProperties(Inner, Outer: TWPTextStyle;
ApplyMode : TWPParagraphBordApply;
Mode : TWPUpdateBorderProperties =
[wpSetBorderFlags, wpSetBorderType, wpSetBorderWidth, wpSetBorderColor, wpSetParColor, wpSetParShading]);
This method is mainly used by the new border dialog.
+ new flags in ViewOptionsEx:
wpShowCurrentCellAsSelected, // Displays current cell to be selected. Disables current selection
wpShowCurrentRowAsSelected, // Displays current table row to be selected. Disables current selection
wpShowCurrentTableAsSelected // Displays current table to be selected. Disables current selection
+ new property YOffsetNormal to defined an upper border for normal and wordwrap view.
* feature Header.MarginMirror changed to work like MS Word
new flag: wpMarginMirrorBookPrint in FormatOptionsEx2 to enable the previous logic
* workaround for MouseWheel UP beeing triggered too often (10 mms check)
+ ClipboardOption wpcoDontPasteHTML to disable HTML pasting completely (avoid problems with firefox)
+ it is now possible to load base64 embedded JPEGs from HTML
* it is now possible to change width of tables which exceed right margin
- fix bug in HTML writer for lists in table cells
- fix in RTF writer to write character colors also for text which is using a character style
- fix: numbering was not always updated
- fix: better use fonts in certain RTF files written by MS Word
+ Update to WPLanguageControl to make Localization easier to use. Only< this code is not required:
procedure TForm1.WPLanguageControl1Loaded(Sender: T
Object
);
begin
WPLangInterface := TWPLocalizationInterface.Create(WPLanguageControl1);
WPLocalizeAllStrings;
6.5.2010 - WPTools 6.11.2
* improvement to border rendering
* improvement to XML unit WPIOXML1 (Premium)
5.5.2010 - WPTools 6.11.1
+ ConvertTableToText now supports option to also handle soft line breaks
- fix problems with underlines at end of line
- fix problem when loading hyperlinks in RTF
- fix problem when saving
attribute
s to XML (WPTools Premium)
- fix problem with text rendering
- fix problem with tables which habe header row and pages with different header margin
19.4.2010 - WPTools 6.11
+ EditOptionEx: wpRowMultiSelect
+ new event: OnInternPaintPar
+ new event: RTFDataCollection AfterApplyUndo
Object
14.4.2010 - WPTools 6.10.6
* fields are now passed as unicode strings to PDF exporter
* Delphi 2010/2009 import has been improved to load unicode values which are stroed in fields.
5.4.2010 - WPTools 6.10.5
+ flag: wpDontExtendSelectionToRightMargin. Do not extend selection to end of line
+ wpInvertActiveRow in ViewOptionsEx
- some fixes for Delphi 2009 and Delphi 2010
- fix for section support
- WPTools premium: Fix for images in text boxes
- workwaround to load RTF which use emfblib for pngblib
28.2.2010 - WPTools 6.10
- improve word left/right movement to skip hidden text
- improve http load of images
- improve support for numbering
- improve saving of character style
attribute
s
11.2.2010 - WPTools 6.09.1
+ LoadFromString now has a "WithClear" parameter
- fix in RTF reader to better load files which do not define codepage
- fix in paint routine to solve a rare lockup
- fix in format routine to improve section support
6.2.2010 - WPTools 6.09.1
- fix of problem in save routine when footnotes were used (WPTools premium)
- fix in HTML writer
- Image optiions now have a Rotation property which allows 90, 180 and 270 setting.
- HTML reader and writer now support different colors for left,right,top, bottom lines
1.2.2010 - WPTools 6.09
- HTML writer will write 8 hard spaces for TAB stops at the start of a paragraph
- HTML writer will write page information only if -PageInfo was used in format string
- fix problem with left aligned text and image wrap around (wrong alignment)
- fix problem with sometimes duplicated images in PDF export
- fix problem with black rectangle in first line under Windows 7, 64 bit
- Improvment to RTF reader to ignore section properties which are not followed by \sect
14.12.2009 - WPTools 6.08
- graphics are resized to fill text area
- fixed problems in numbering
- fixed problem with one word paragraphs in justified paragraphs
- other improvements in editor
27.10.2009 - WPTools 6.07
- fixed one leaking TList per TWPRichText
* improved layout of most important dialogs
* improved extended insert symbol dialog
- fix in RTF reader to load sections and header+footer written by Word 2003
- don't add unwanted cell padding when loading table cells
- fix in WPTools reader to read custom number styles
- if paragraph styles use number styles the indent defined in the style has priority over numberstyle
- LabelDef now also works for one row and one column
- better handling of mousewheel event
- fix for tabs in tables
+ GIF animation (requires GifImage) library (not threaded)
to use it You need to set ViewOption wpUseOwnDoubleBuffer and call
the method RefreshAniImages using a timer
object
.
- fix problem when sections were used with LabeDef.Active = true
* change in HTML writer to close tags before paragraph end
4.10.2009 - WPTools 6.06
- fix problem with Delphi 2010 support (language control)
- fix problem with PDF export to reduce PDF size
- improve support for IME
* improve AsWebPage format mode. Now WordWrap propery is supported.
- fix searching text upwards with "Whole Word" selected
+ RTF writer now can use format option "-writehighlight" to writh \highlight instead of \cb
14.9.2009 - WPTools 6.05.9
+ added Delphi 2010 Support
- problem when pasting from "The Bat"
- fix problem in Delphi 2009 (Assign Method)
3.8.2009 - WPTools 6.05.8'
* when using "Delete All" in the tabstop dialog, all tabs will be cleared
added to manual: Tabstop Category
- fixed problem when deleting text in a paragraph. The alignment was cleared unexpectedly.
- fix problem with installer, WPMangeHeaderFooter.DFM was not included
- fix for IPara in mail merge field
object
s
- improved handling of hoover effect for hyperlinks
- improved text rendering for wPDF output (CID Mode)
- add correct WPManHeadFoot.dfm
23.7.2009 - WPTools 6.05.7
+ WRITE_PROP_WPTOOLSOBJ $define in WPIOWriteRTF. Avoid problems when saving RTF and opting in Word
In case of special
object
s, such as SPAN codes, \*\wpfldinst is beeing written what is ignored by WOrd
+ Dialog HeaderFooter can optionally create and manage header&footer for the current section
+ new KeepN Handling. This is by default activated in FormatOptionsEx2
+ new wpfHideParagraphWithHiddenText in FormatOptionsEx2.
Now paragraphs will be hidden if empty or only contain hidden text.
+ new format option -zap1 will remove the every first byte to convert a two byte stream into singly byte
-zap2 will remove every second byte. Usie this option when loading data from unicode data sets
- bugfix for table loading in RTF
15.7.2009 - WPTools 6.05
+ TParagraph.Trim method to remove white spaces at start and end
+ Vertical Scrolling by pressing the middle mouse button now works.
+ improved auto thumbnail mode
* enhancement to HTML reader / writer to handle embedded SPAN
object
s
+ new method: ApplySPANStyles(and_remove : Boolean=false; ignore_charattr : Boolean = false);
can be used to apply SPAN styles to the text which it embeds
+ The
function
InputSpan
Object
s(
Attribute
s : TWPAbstractCharAttrInterface ) : TWPTextObj;
can be used to wrap the selected text into SPAN
object
s
+ method LoadCSSheet can be used to load paragraph styles in CSS format from a string
- fix problem with Wordrwap and centered text
+ new even OnText
Object
MovePosition (move event) - OnText
Object
Move is still used for resize (unchanged)
28.6.2009 - WPTools 6.04
+ WPTools Premium: Column Balancing
* many improvements in RTF reader. Word
document
s are now understood better
* Improvement in check for protected text (ppMergedText)
+ new ViewOptionsEx property
- auto hyperlinks were not working
+ TWPComboBox has an event OnUpdateItems which will be triggered after the items had been automatically assigned.
24.6.2009 - WPTools 6.03.6
* thinner page borders in thumbnail mode.
ViewOptionsEx: wpAutoThumbnailMode will show pagenumbers only when in thubmbnail mode (= wpShowPageNRinGap in ViewOptions)
+ property ColorDesktop and DeskGradientHorizontal to render the background with a gradient fill
* fix for protected text handling (CR after a field)
* fix for text alignment near a movable image
- EditOption AutoDetectHyperlinks was not working anymore
* WPReporter: SuperMerge.Stack.PageBreakAFterGroup := true was ot working when footers were used
1.6.2009 - WPTools 6.03.5
- fix problem with display of character
attribute
s when
attribute
s were inherited from paragraph styles
- fix problems with selection deletion in single column, single row tables
- improvement of RTF writer when writing sections
11.5.2009 - WPTools 6.03.3
- improved report band dialog, new ShowOptions property
- fix in RTF reader to load header/footer
- change in HTML writer to save SPAN instead of FONT tag
- several fixes in editor
* WPTools Premium: better column support. Fixed column height now splits correctly on 2 pages.
28.4.2009 - WPTools 6.03.2
- fix problem with justified text in PDF
21.4.2009 - WPTools 6.03.1
- fix problem with images when used in Delphi 2009
- better support for header/footer in RTF files created by word. (Ignore bogus header/footer)
- soome stability fixes
25.3.2009 - WPTools 6.03
+ improved text rendering - optimation for character distances on screen to provide better display
+ improvement on ShowMergeFieldNames to improve cursor movement and drag and drop
+ automatic disable dragging of fields inside of fields
+ improved merge field selection. Text
Object
.Selected
Object
now returns the mergefield if it was completely selected
+ change in HTML saving code to save src in after width adn height (for outlook)
* various bugfixes
17.1.2009 - WPTools 6.02
- WPPREMIUM: Text after Columns initialized with WPAT_COLUMNS_Y is now allowed
+ TWPToolBar FontName drop down now lists fonts used by
document
first
- fix for tables which use a fixed row height and are splitted on different pages
+ improvements necessary for Delphi 2009 - the Locaization demo now works
+ EditOptionEx wpDontPreserve
Object
sAgainstDeletion
- fix problem in Image
Object
LoadFromStream when GraphicEx is used
- fix problem with Delphi 2009 when loading WPReporter templates
- fix problem with HTML reader with paragraph style of first paragraph
+ GetFontUnicodeRanges dynamically linked to avoid Win98 problem
26.10.2008 - WPTools 6.01
* updated HTTP Demo, now with "Source View"
+ DELETE/BACKSPC at start of line removes right/center alignment
+ loads background images for paragraphs, tables and styles
* improvement to text protection (empty lines)
- improvements to HTML and CSS reader
- improved HTML format routine
- improved MIME loading - now supports binary data despite Synapse does not)
+ MIME reader capturesHTML body for SourceVIew
* DataProvider now uses MergeText('',true) instead of MergeText
+ boolean wphttp_Disable to disconnect HTTP temporarily
* several changes to improve compatibility with Delphi 2009
17.10.2008 - WPTools 6.00.1
- several changes to fix problems which occured with use of Delphi 2009
* update to WPIO_MIME to also load binary encoded GIFS and JPEGS from EML files
16.10.2008 - WPTools 6.00
* new installer
+ VCL demo
* fixed problem with TWPComboBox and default
attribute
s
* updated "Tasks\Mini" demo project
- fix in default actions
3.10.2008 - WPTools 6 Beta c
- remove not required unit reference form WPRTEReport
* update to manual
2.10.2008 - WPTools 6 Beta b
- fix in Installation script
30.9.2008 - WPTools 6 Beta a
1) Application-Server-Mode
WPTools 6 introduces a feature which is called "Application-Server-Mode". This is only available when you have the PREMIUM version. This mode is activated when true is assigned to the global boolean variable WPAppServerMode. When this mode is activated the editor does not use the double buffered output anymore. While this can cause some flickering the network traffic is reduced when the application runs on an application server, such as Citrix.
Please note, effective with WPTools 6, software which was written for application servers (such as clinic software) may only be distributed when a TEAM or SITE license of WPTools was acquired.END
2) Integrated Label Printing
When activated the integrated label printing shows multiple labels on one virtual sheet of paper. The cursor can move from label to label freely. The user can so edit the labels, add new or delete unwanted labels before the complete sheet is printed. This is a very unique and versatile feature.
3) Additions to the PDF export with wPDF V3
Create embedded data
object
s
Create edit / memo fields
Create a check box field
Also the creation of PDF tags was enhanced. So now hints to paragraph styles will be also exported.
4) Additional Control over Clipboard Actions
a) properties to select the format
b) added security
5) Added Section API
+ WPRichText1.ActiveSection
+ WPRichText1.InputSection
+ TWPPagePropDlg has new method ExecuteEx. Use it to change current page size or Section
WPPagePropDlg1.ExecuteEx(WPRichText1.ActiveSection);
6) Load over HTTP connections (requires Synapse)
7) Load and save MIME encoded HTML with embedded images (requires Synapse)
8) Integrated XML syntax highlighting (non destructive)
9) WPReporter: Token to Template conversion with syntax highlighting
10) Additions to paragraph API
11) Special mode to format HTML
document
s
Table of Contents
Header Files The #define Guard Header File Dependencies Inline
Function
s The -inl.h Files
Function
Parameter Ordering Names and Order of Includes
Scoping Namespaces Nested Classes Nonmember, Static Member, and Global
Function
s Local Variables Static and Global Variables
Classes Doing Work in Constructors Default Constructors Explicit Constructors Copy Constructors Structs vs. Classes Inheritance Multiple Inheritance Interfaces Operator Overloading Access Control Declaration Order Write Short
Function
s
Google-Specific Magic Smart Pointers cpplint
Other C++ Features Reference Arguments
Function
Overloading Default Arguments Variable-Length Arrays and alloca() Friends Exceptions Run-Time Type Information (RTTI) Casting Streams Preincrement and Predecrement Use of const Integer Types 64-bit Portability Preprocessor Macros 0 and NULL sizeof Boost C++0x
Naming General Naming Rules File Names Type Names Variable Names Constant Names
Function
Names Namespace Names Enumerator Names Macro Names Exceptions to Naming Rules
Comments Comment Style File Comments Class Comments
Function
Comments Variable Comments Implementation Comments Punctuation, Spelling and Grammar TODO Comments Deprecation Comments
Formatting Line Length Non-ASCII Characters Spaces vs. Tabs
Function
Declarations and Definitions
Function
Calls Conditionals Loops and Switch Statements Pointer and Reference Expressions Boolean Expressions Return Values Variable and Array Initialization Preprocessor Directives Class Format Constructor Initializer Lists Namespace Formatting Horizontal Whitespace Vertical Whitespace
Exceptions to the Rules Existing Non-conformant Code Windows Code
Important Note
Displaying Hidden Details in this Guide
link ▶This style guide contains many details that are initially hidden from view. They are marked by the triangle icon, which you see here on your left. Click it now. You should see "Hooray" appear below.
Hooray! Now you know you can expand points to get more details. Alternatively, there's an "expand all" at the top of this
document
.
Background
C++ is the main development language used by many of Google's open-source projects. As every C++ programmer knows, the language has many powerful features, but this power brings with it complexity, which in turn can make code more bug-prone and harder to read and maintain.
The goal of this guide is to manage this complexity by describing in detail the dos and don'ts of writing C++ code. These rules exist to keep the code base manageable while still allowing coders to use C++ language features productively.
Style, also known as readability, is what we call the conventions that govern our C++ code. The term Style is a bit of a misnomer, since these conventions cover far more than just source file formatting.
One way in which we keep the code base manageable is by enforcing consistency. It is very important that any programmer be able to look at another's code and quickly understand it. Maintaining a uniform style and following conventions means that we can more easily use "pattern-matching" to infer what various symbols are and what invariants are true about them. Creating common, required idioms and patterns makes code much easier to understand. In some cases there might be good arguments for changing certain style rules, but we nonetheless keep things as they are in order to preserve consistency.
Another issue this guide addresses is that of C++ feature bloat. C++ is a huge language with many advanced features. In some cases we constrain, or even ban, use of certain features. We do this to keep code simple and to avoid the various common errors and problems that these features can cause. This guide lists these features and explains why their use is restricted.
Open-source projects developed by Google conform to the requirements in this guide.
Note that this guide is not a C++ tutorial: we assume that the reader is familiar with the language.
Header Files
In general, every .cc file should have an associated .h file. There are some common exceptions, such as unittests and small .cc files containing just a main()
function
.
Correct use of header files can make a huge difference to the readability, size and performance of your code.
The following rules will guide you through the various pitfalls of using header files.
The #define Guard
link ▶All header files should have #define guards to prevent multiple inclusion. The format of the symbol name should be ___H_.
To guarantee uniqueness, they should be based on the full path in a project's source tree. For example, the file foo/src/bar/baz.h in project foo should have the following guard:
#ifndef FOO_BAR_BAZ_H_
#define FOO_BAR_BAZ_H_
#endif // FOO_BAR_BAZ_H_
Header File Dependencies
link ▶Don't use an #include when a forward declaration would suffice.
When you include a header file you introduce a dependency that will cause your code to be recompiled whenever the header file changes. If your header file includes other header files, any change to those files will cause any code that includes your header to be recompiled. Therefore, we prefer to minimize includes, particularly includes of header files in other header files.
You can significantly minimize the number of header files you need to include in your own header files by using forward declarations. For example, if your header file uses the File class in ways that do not require access to the declaration of the File class, your header file can just forward declare class File; instead of having to #include "file/base/file.h".
How can we use a class Foo in a header file without access to its definition?
We can declare data members of type Foo* or Foo&.
We can declare (but not define)
function
s with arguments, and/or return values, of type Foo. (One exception is if an argument Foo or const Foo& has a non-explicit, one-argument constructor, in which case we need the full definition to support automatic type conversion.)
We can declare static data members of type Foo. This is because static data members are defined outside the class definition.
On the other hand, you must include the header file for Foo if your class subclasses Foo or has a data member of type Foo.
Sometimes it makes sense to have pointer (or better, scoped_ptr) members instead of
object
members. However, this complicates code readability and imposes a performance penalty, so avoid doing this transformation if the only purpose is to minimize includes in header files.
Of course, .cc files typically do require the definitions of the classes they use, and usually have to include several header files.
Note: If you use a symbol Foo in your source file, you should bring in a definition for Foo yourself, either via an #include or via a forward declaration. Do not depend on the symbol being brought in transitively via headers not directly included. One exception is if Foo is used in myfile.cc, it's ok to #include (or forward-declare) Foo in myfile.h, instead of myfile.cc.
Inline
Function
s
link ▶Define
function
s inline only when they are small, say, 10 lines or less.
Definition:
You can declare
function
s in a way that allows the compiler to expand them inline rather than calling them through the usual
function
call mechanism.
Pros:
Inlining a
function
can generate more efficient
object
code, as long as the inlined
function
is small. Feel free to inline accessors and mutators, and other short, performance-critical
function
s.
Cons:
Overuse of inlining can actually make programs slower. Depending on a
function
's size, inlining it can cause the code size to increase or decrease. Inlining a very small accessor
function
will usually decrease code size while inlining a very large
function
can dramatically increase code size. On modern processors smaller code usually runs faster due to better use of the instruction cache.
Decision:
A decent rule of thumb is to not inline a
function
if it is more than 10 lines long. Beware of destructors, which are often longer than they appear because of implicit member- and base-destructor calls!
Another useful rule of thumb: it's typically not cost effective to inline
function
s with loops or switch statements (unless, in the common case, the loop or switch statement is never executed).
It is important to know that
function
s are not always inlined even if they are declared as such; for example, virtual and recursive
function
s are not normally inlined. Usually recursive
function
s should not be inline. The main reason for making a virtual
function
inline is to place its definition in the class, either for convenience or to
document
its behavior, e.g., for accessors and mutators.
The -inl.h Files
link ▶You may use file names with a -inl.h suffix to define complex inline
function
s when needed.
The definition of an inline
function
needs to be in a header file, so that the compiler has the definition available for inlining at the call sites. However, implementation code properly belongs in .cc files, and we do not like to have much actual code in .h files unless there is a readability or performance advantage.
If an inline
function
definition is short, with very little, if any, logic in it, you should put the code in your .h file. For example, accessors and mutators should certainly be inside a class definition. More complex inline
function
s may also be put in a .h file for the convenience of the implementer and callers, though if this makes the .h file too unwieldy you can instead put that code in a separate -inl.h file. This separates the implementation from the class definition, while still allowing the implementation to be included where necessary.
Another use of -inl.h files is for definitions of
function
templates. This can be used to keep your template definitions easy to read.
Do not forget that a -inl.h file requires a #define guard just like any other header file.
Function
Parameter Ordering
link ▶When defining a
function
, parameter order is: inputs, then outputs.
Parameters to C/C++
function
s are either input to the
function
, output from the
function
, or both. Input parameters are usually values or const references, while output and input/output parameters will be non-const pointers. When ordering
function
parameters, put all input-only parameters before any output parameters. In particular, do not add new parameters to the end of the
function
just because they are new; place new input-only parameters before the output parameters.
This is not a hard-and-fast rule. Parameters that are both input and output (often classes/structs) muddy the waters, and, as always, consistency with related
function
s may require you to bend the rule.
Names and Order of Includes
link ▶Use standard order for readability and to avoid hidden dependencies: C library, C++ library, other libraries' .h, your project's .h.
All of a project's header files should be listed as descentants of the project's source directory without use of UNIX directory shortcuts . (the current directory) or .. (the parent directory). For example, google-awesome-project/src/base/logging.h should be included as
#include "base/logging.h"
In dir/foo.cc, whose main purpose is to implement or test the stuff in dir2/foo2.h, order your includes as follows:
dir2/foo2.h (preferred location — see details below).
C system files.
C++ system files.
Other libraries' .h files.
Your project's .h files.
The preferred ordering reduces hidden dependencies. We want every header file to be compilable on its own. The easiest way to achieve this is to make sure that every one of them is the first .h file #included in some .cc.
dir/foo.cc and dir2/foo2.h are often in the same directory (e.g. base/basictypes_test.cc and base/basictypes.h), but can be in different directories too.
Within each section it is nice to order the includes alphabetically.
For example, the includes in google-awesome-project/src/foo/internal/fooserver.cc might look like this:
#include "foo/public/fooserver.h" // Preferred location.
#include
#include
#include
#include
#include "base/basictypes.h"
#include "base/commandlineflags.h"
#include "foo/public/bar.h"
Scoping
Namespaces
link ▶Unnamed namespaces in .cc files are encouraged. With named namespaces, choose the name based on the project, and possibly its path. Do not use a using-directive.
Definition:
Namespaces subdivide the global scope into distinct, named scopes, and so are useful for preventing name collisions in the global scope.
Pros:
Namespaces provide a (hierarchical) axis of naming, in addition to the (also hierarchical) name axis provided by classes.
For example, if two different projects have a class Foo in the global scope, these symbols may collide at compile time or at runtime. If each project places their code in a namespace, project1::Foo and project2::Foo are now distinct symbols that do not collide.
Cons:
Namespaces can be confusing, because they provide an additional (hierarchical) axis of naming, in addition to the (also hierarchical) name axis provided by classes.
Use of unnamed spaces in header files can easily cause violations of the C++ One Definition Rule (ODR).
Decision:
Use namespaces according to the policy described below.
Unnamed Namespaces
Unnamed namespaces are allowed and even encouraged in .cc files, to avoid runtime naming conflicts:
namespace { // This is in a .cc file.
// The content of a namespace is not indented
enum { kUnused, kEOF, kError }; // Commonly used tokens.
bool AtEof() { return pos_ == kEOF; } // Uses our namespace's EOF.
} // namespace
However, file-scope declarations that are associated with a particular class may be declared in that class as types, static data members or static member
function
s rather than as members of an unnamed namespace. Terminate the unnamed namespace as shown, with a comment // namespace.
Do not use unnamed namespaces in .h files.
Named Namespaces
Named namespaces should be used as follows:
Namespaces wrap the entire source file after includes, gflags definitions/declarations, and forward declarations of classes from other namespaces:
// In the .h file
namespace mynamespace {
// All declarations are within the namespace scope.
// Notice the lack of indentation.
class MyClass {
public:
void Foo();
} // namespace mynamespace
// In the .cc file
namespace mynamespace {
// Definition of
function
s is within scope of the namespace.
void MyClass::Foo() {
} // namespace mynamespace
The typical .cc file might have more complex detail, including the need to reference classes in other namespaces.
#include "a.h"
DEFINE_bool(someflag, false, "dummy flag");
class C; // Forward declaration of class C in the global namespace.
namespace a { class A; } // Forward declaration of a::A.
namespace b {
...code for b... // Code goes against the left margin.
} // namespace b
Do not declare anything in namespace std, not even forward declarations of standard library classes. Declaring entities in namespace std is undefined behavior, i.e., not portable. To declare entities from the standard library, include the appropriate header file.
You may not use a using-directive to make all names from a namespace available.
// Forbidden -- This pollutes the namespace.
using namespace foo;
You may use a using-declaration anywhere in a .cc file, and in
function
s, methods or classes in .h files.
// OK in .cc files.
// Must be in a
function
, method or class in .h files.
using ::foo::bar;
Namespace aliases are allowed anywhere in a .cc file, anywhere inside the named namespace that wraps an entire .h file, and in
function
s and methods.
// Shorten access to some commonly used names in .cc files.
namespace fbz = ::foo::bar::baz;
// Shorten access to some commonly used names (in a .h file).
namespace librarian {
// The following alias is available to all files including
// this header (in namespace librarian):
// alias names should therefore be chosen consistently
// within a project.
namespace pd_s = ::pipeline_diagnostics::sidetable;
inline void my_inline_
function
() {
// namespace alias local to a
function
(or method).
namespace fbz = ::foo::bar::baz;
} // namespace librarian
Note that an alias in a .h file is visible to everyone #including that file, so public headers (those available outside a project) and headers transitively #included by them, should avoid defining aliases, as part of the general goal of keeping public APIs as small as possible.
Nested Classes
link ▶Although you may use public nested classes when they are part of an interface, consider a namespace to keep declarations out of the global scope.
Definition:
A class can define another class within it; this is also called a member class.
class Foo {
private:
// Bar is a member class, nested within Foo.
class Bar {
Pros:
This is useful when the nested (or member) class is only used by the enclosing class; making it a member puts it in the enclosing class scope rather than polluting the outer scope with the class name. Nested classes can be forward declared within the enclosing class and then defined in the .cc file to avoid including the nested class definition in the enclosing class declaration, since the nested class definition is usually only relevant to the implementation.
Cons:
Nested classes can be forward-declared only within the definition of the enclosing class. Thus, any header file manipulating a Foo::Bar* pointer will have to include the full class declaration for Foo.
Decision:
Do not make nested classes public unless they are actually part of the interface, e.g., a class that holds a set of options for some method.
Nonmember, Static Member, and Global
Function
s
link ▶Prefer nonmember
function
s within a namespace or static member
function
s to global
function
s; use completely global
function
s rarely.
Pros:
Nonmember and static member
function
s can be useful in some situations. Putting nonmember
function
s in a namespace avoids polluting the global namespace.
Cons:
Nonmember and static member
function
s may make more sense as members of a new class, especially if they access external resources or have significant dependencies.
Decision:
Sometimes it is useful, or even necessary, to define a
function
not bound to a class instance. Such a
function
can be either a static member or a nonmember
function
. Nonmember
function
s should not depend on external variables, and should nearly always exist in a namespace. Rather than creating classes only to group static member
function
s which do not share static data, use namespaces instead.
Function
s defined in the same compilation unit as production classes may introduce unnecessary coupling and link-time dependencies when directly called from other compilation units; static member
function
s are particularly susceptible to this. Consider extracting a new class, or placing the
function
s in a namespace possibly in a separate library.
If you must define a nonmember
function
and it is only needed in its .cc file, use an unnamed namespace or static linkage (eg static int Foo() {...}) to limit its scope.
Local Variables
link ▶Place a
function
's variables in the narrowest scope possible, and initialize variables in the declaration.
C++ allows you to declare variables anywhere in a
function
. We encourage you to declare them in as local a scope as possible, and as close to the first use as possible. This makes it easier for the reader to find the declaration and see what type the variable is and what it was initialized to. In particular, initialization should be used instead of declaration and assignment, e.g.
int i;
i = f(); // Bad -- initialization separate from declaration.
int j = g(); // Good -- declaration has initialization.
Note that gcc implements for (int i = 0; i < 10; ++i) correctly (the scope of i is only the scope of the for loop), so you can then reuse i in another for loop in the same scope. It also correctly scopes declarations in if and while statements, e.g.
while (const char* p = strchr(str, '/')) str = p + 1;
There is one caveat: if the variable is an
object
, its constructor is invoked every time it enters scope and is created, and its destructor is invoked every time it goes out of scope.
// Inefficient implementation:
for (int i = 0; i < 1000000; ++i) {
Foo f; // My ctor and dtor get called 1000000 times each.
f.DoSomething(i);
It may be more efficient to declare such a variable used in a loop outside that loop:
Foo f; // My ctor and dtor get called once each.
for (int i = 0; i < 1000000; ++i) {
f.DoSomething(i);
Static and Global Variables
link ▶Static or global variables of class type are forbidden: they cause hard-to-find bugs due to indeterminate order of construction and destruction.
Object
s with static storage duration, including global variables, static variables, static class member variables, and
function
static variables, must be Plain Old Data (POD): only ints, chars, floats, or pointers, or arrays/structs of POD.
The order in which class constructors and initializers for static variables are called is only partially specified in C++ and can even change from build to build, which can cause bugs that are difficult to find. Therefore in addition to banning globals of class type, we do not allow static POD variables to be initialized with the result of a
function
, unless that
function
(such as getenv(), or getpid()) does not itself depend on any other globals.
Likewise, the order in which destructors are called is defined to be the reverse of the order in which the constructors were called. Since constructor order is indeterminate, so is destructor order. For example, at program-end time a static variable might have been destroyed, but code still running -- perhaps in another thread -- tries to access it and fails. Or the destructor for a static 'string' variable might be run prior to the destructor for another variable that contains a reference to that string.
As a result we only allow static variables to contain POD data. This rule completely disallows vector (use C arrays instead), or string (use const char []).
If you need a static or global variable of a class type, consider initializing a pointer (which will never be freed), from either your main()
function
or from pthread_once(). Note that this must be a raw pointer, not a "smart" pointer, since the smart pointer's destructor will have the order-of-destructor issue that we are trying to avoid.
Classes
Classes are the fundamental unit of code in C++. Naturally, we use them extensively. This section lists the main dos and don'ts you should follow when writing a class.
Doing Work in Constructors
link ▶In general, constructors should merely set member variables to their initial values. Any complex initialization should go in an explicit Init() method.
Definition:
It is possible to perform initialization in the body of the constructor.
Pros:
Convenience in typing. No need to worry about whether the class has been initialized or not.
Cons:
The problems with doing work in constructors are:
There is no easy way for constructors to signal errors, short of using exceptions (which are forbidden).
If the work fails, we now have an
object
whose initialization code failed, so it may be an indeterminate state.
If the work calls virtual
function
s, these calls will not get dispatched to the subclass implementations. Future modification to your class can quietly introduce this problem even if your class is not currently subclassed, causing much confusion.
If someone creates a global variable of this type (which is against the rules, but still), the constructor code will be called before main(), possibly breaking some implicit assumptions in the constructor code. For instance, gflags will not yet have been initialized.
Decision:
If your
object
requires non-trivial initialization, consider having an explicit Init() method. In particular, constructors should not call virtual
function
s, attempt to raise errors, access potentially uninitialized global variables, etc.
Default Constructors
link ▶You must define a default constructor if your class defines member variables and has no other constructors. Otherwise the compiler will do it for you, badly.
Definition:
The default constructor is called when we new a class
object
with no arguments. It is always called when calling new[] (for arrays).
Pros:
Initializing structures by default, to hold "impossible" values, makes debugging much easier.
Cons:
Extra work for you, the code writer.
Decision:
If your class defines member variables and has no other constructors you must define a default constructor (one that takes no arguments). It should preferably initialize the
object
in such a way that its internal state is consistent and valid.
The reason for this is that if you have no other constructors and do not define a default constructor, the compiler will generate one for you. This compiler generated constructor may not initialize your
object
sensibly.
If your class inherits from an existing class but you add no new member variables, you are not required to have a default constructor.
Explicit Constructors
link ▶Use the C++ keyword explicit for constructors with one argument.
Definition:
Normally, if a constructor takes one argument, it can be used as a conversion. For instance, if you define Foo::Foo(string name) and then pass a string to a
function
that expects a Foo, the constructor will be called to convert the string into a Foo and will pass the Foo to your
function
for you. This can be convenient but is also a source of trouble when things get converted and new
object
s created without you meaning them to. Declaring a constructor explicit prevents it from being invoked implicitly as a conversion.
Pros:
Avoids undesirable conversions.
Cons:
None.
Decision:
We require all single argument constructors to be explicit. Always put explicit in front of one-argument constructors in the class definition: explicit Foo(string name);
The exception is copy constructors, which, in the rare cases when we allow them, should probably not be explicit. Classes that are intended to be transparent wrappers around other classes are also exceptions. Such exceptions should be clearly marked with comments.
Copy Constructors
link ▶Provide a copy constructor and assignment operator only when necessary. Otherwise, disable them with DISALLOW_COPY_AND_ASSIGN.
Definition:
The copy constructor and assignment operator are used to create copies of
object
s. The copy constructor is implicitly invoked by the compiler in some situations, e.g. passing
object
s by value.
Pros:
Copy constructors make it easy to copy
object
s. STL containers require that all contents be copyable and assignable. Copy constructors can be more efficient than CopyFrom()-style workarounds because they combine construction with copying, the compiler can elide them in some contexts, and they make it easier to avoid heap allocation.
Cons:
Implicit copying of
object
s in C++ is a rich source of bugs and of performance problems. It also reduces readability, as it becomes hard to track which
object
s are being passed around by value as opposed to by reference, and therefore where changes to an
object
are reflected.
Decision:
Few classes need to be copyable. Most should have neither a copy constructor nor an assignment operator. In many situations, a pointer or reference will work just as well as a copied value, with better performance. For example, you can pass
function
parameters by reference or pointer instead of by value, and you can store pointers rather than
object
s in an STL container.
If your class needs to be copyable, prefer providing a copy method, such as CopyFrom() or Clone(), rather than a copy constructor, because such methods cannot be invoked implicitly. If a copy method is insufficient in your situation (e.g. for performance reasons, or because your class needs to be stored by value in an STL container), provide both a copy constructor and assignment operator.
If your class does not need a copy constructor or assignment operator, you must explicitly disable them. To do so, add dummy declarations for the copy constructor and assignment operator in the private: section of your class, but do not provide any corresponding definition (so that any attempt to use them results in a link error).
For convenience, a DISALLOW_COPY_AND_ASSIGN macro can be used:
// A macro to disallow the copy constructor and operator=
function
s
// This should be used in the private: declarations for a class
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
TypeName(const TypeName&); \
void operator=(const TypeName&)
Then, in class Foo:
class Foo {
public:
Foo(int f);
~Foo();
private:
DISALLOW_COPY_AND_ASSIGN(Foo);
Structs vs. Classes
link ▶Use a struct only for passive
object
s that carry data; everything else is a class.
The struct and class keywords behave almost identically in C++. We add our own semantic meanings to each keyword, so you should use the appropriate keyword for the data-type you're defining.
structs should be used for passive
object
s that carry data, and may have associated constants, but lack any
function
ality other than access/setting the data members. The accessing/setting of fields is done by directly accessing the fields rather than through method invocations. Methods should not provide behavior but should only be used to set up the data members, e.g., constructor, destructor, Initialize(), Reset(), Validate().
If more
function
ality is required, a class is more appropriate. If in doubt, make it a class.
For consistency with STL, you can use struct instead of class for functors and traits.
Note that member variables in structs and classes have different naming rules.
Inheritance
link ▶Composition is often more appropriate than inheritance. When using inheritance, make it public.
Definition:
When a sub-class inherits from a base class, it includes the definitions of all the data and operations that the parent base class defines. In practice, inheritance is used in two major ways in C++: implementation inheritance, in which actual code is inherited by the child, and interface inheritance, in which only method names are inherited.
Pros:
Implementation inheritance reduces code size by re-using the base class code as it specializes an existing type. Because inheritance is a compile-time declaration, you and the compiler can understand the operation and detect errors. Interface inheritance can be used to programmatically enforce that a class expose a particular API. Again, the compiler can detect errors, in this case, when a class does not define a necessary method of the API.
Cons:
For implementation inheritance, because the code implementing a sub-class is spread between the base and the sub-class, it can be more difficult to understand an implementation. The sub-class cannot override
function
s that are not virtual, so the sub-class cannot change implementation. The base class may also define some data members, so that specifies physical layout of the base class.
Decision:
All inheritance should be public. If you want to do private inheritance, you should be including an instance of the base class as a member instead.
Do not overuse implementation inheritance. Composition is often more appropriate. Try to restrict use of inheritance to the "is-a" case: Bar subclasses Foo if it can reasonably be said that Bar "is a kind of" Foo.
Make your destructor virtual if necessary. If your class has virtual methods, its destructor should be virtual.
Limit the use of protected to those member
function
s that might need to be accessed from subclasses. Note that data members should be private.
When redefining an inherited virtual
function
, explicitly declare it virtual in the declaration of the derived class. Rationale: If virtual is omitted, the reader has to check all ancestors of the class in question to determine if the
function
is virtual or not.
Multiple Inheritance
link ▶Only very rarely is multiple implementation inheritance actually useful. We allow multiple inheritance only when at most one of the base classes has an implementation; all other base classes must be pure interface classes tagged with the Interface suffix.
Definition:
Multiple inheritance allows a sub-class to have more than one base class. We distinguish between base classes that are pure interfaces and those that have an implementation.
Pros:
Multiple implementation inheritance may let you re-use even more code than single inheritance (see Inheritance).
Cons:
Only very rarely is multiple implementation inheritance actually useful. When multiple implementation inheritance seems like the solution, you can usually find a different, more explicit, and cleaner solution.
Decision:
Multiple inheritance is allowed only when all superclasses, with the possible exception of the first one, are pure interfaces. In order to ensure that they remain pure interfaces, they must end with the Interface suffix.
Note: There is an exception to this rule on Windows.
Interfaces
link ▶Classes that satisfy certain conditions are allowed, but not required, to end with an Interface suffix.
Definition:
A class is a pure interface if it meets the following requirements:
It has only public pure virtual ("= 0") methods and static methods (but see below for destructor).
It may not have non-static data members.
It need not have any constructors defined. If a constructor is provided, it must take no arguments and it must be protected.
If it is a subclass, it may only be derived from classes that satisfy these conditions and are tagged with the Interface suffix.
An interface class can never be directly instantiated because of the pure virtual method(s) it declares. To make sure all implementations of the interface can be destroyed correctly, they must also declare a virtual destructor (in an exception to the first rule, this should not be pure). See Stroustrup, The C++ Programming Language, 3rd edition, section 12.4 for details.
Pros:
Tagging a class with the Interface suffix lets others know that they must not add implemented methods or non static data members. This is particularly important in the case of multiple inheritance. Additionally, the interface concept is already well-understood by Java programmers.
Cons:
The Interface suffix lengthens the class name, which can make it harder to read and understand. Also, the interface property may be considered an implementation detail that shouldn't be exposed to clients.
Decision:
A class may end with Interface only if it meets the above requirements. We do not require the converse, however: classes that meet the above requirements are not required to end with Interface.
Operator Overloading
link ▶Do not overload operators except in rare, special circumstances.
Definition:
A class can define that operators such as + and / operate on the class as if it were a built-in type.
Pros:
Can make code appear more intuitive because a class will behave in the same way as built-in types (such as int). Overloaded operators are more playful names for
function
s that are less-colorfully named, such as Equals() or Add(). For some template
function
s to work correctly, you may need to define operators.
Cons:
While operator overloading can make code more intuitive, it has several drawbacks:
It can fool our intuition into thinking that expensive operations are cheap, built-in operations.
It is much harder to find the call sites for overloaded operators. Searching for Equals() is much easier than searching for relevant invocations of ==.
Some operators work on pointers too, making it easy to introduce bugs. Foo + 4 may do one thing, while &Foo + 4 does something totally different. The compiler does not complain for either of these, making this very hard to debug.
Overloading also has surprising ramifications. For instance, if a class overloads unary operator&, it cannot safely be forward-declared.
Decision:
In general, do not overload operators. The assignment operator (operator=), in particular, is insidious and should be avoided. You can define
function
s like Equals() and CopyFrom() if you need them. Likewise, avoid the dangerous unary operator& at all costs, if there's any possibility the class might be forward-declared.
However, there may be rare cases where you need to overload an operator to interoperate with templates or "standard" C++ classes (such as operator<<(ostream&, const T&) for logging). These are acceptable if fully justified, but you should try to avoid these whenever possible. In particular, do not overload operator== or operator< just so that your class can be used as a key in an STL container; instead, you should create equality and comparison functor types when declaring the container.
Some of the STL algorithms do require you to overload operator==, and you may do so in these cases, provided you
document
why.
See also Copy Constructors and
Function
Overloading.
Access Control
link ▶Make data members private, and provide access to them through accessor
function
s as needed (for technical reasons, we allow data members of a test fixture class to be protected when using Google Test). Typically a variable would be called foo_ and the accessor
function
foo(). You may also want a mutator
function
set_foo(). Exception: static const data members (typically called kFoo) need not be private.
The definitions of accessors are usually inlined in the header file.
See also Inheritance and
Function
Names.
Declaration Order
link ▶Use the specified order of declarations within a class: public: before private:, methods before data members (variables), etc.
Your class definition should start with its public: section, followed by its protected: section and then its private: section. If any of these sections are empty, omit them.
Within each section, the declarations generally should be in the following order:
Typedefs and Enums
Constants (static const data members)
Constructors
Destructor
Methods, including static methods
Data Members (except static const data members)
Friend declarations should always be in the private section, and the DISALLOW_COPY_AND_ASSIGN macro invocation should be at the end of the private: section. It should be the last thing in the class. See Copy Constructors.
Method definitions in the corresponding .cc file should be the same as the declaration order, as much as possible.
Do not put large method definitions inline in the class definition. Usually, only trivial or performance-critical, and very short, methods may be defined inline. See Inline
Function
s for more details.
Write Short
Function
s
link ▶Prefer small and focused
function
s.
We recognize that long
function
s are sometimes appropriate, so no hard limit is placed on
function
s length. If a
function
exceeds about 40 lines, think about whether it can be broken up without harming the structure of the program.
Even if your long
function
works perfectly now, someone modifying it in a few months may add new behavior. This could result in bugs that are hard to find. Keeping your
function
s short and simple makes it easier for other people to read and modify your code.
You could find long and complicated
function
s when working with some code. Do not be intimidated by modifying existing code: if working with such a
function
proves to be difficult, you find that errors are hard to debug, or you want to use a piece of it in several different contexts, consider breaking up the
function
into smaller and more manageable pieces.
Google-Specific Magic
There are various tricks and utilities that we use to make C++ code more robust, and various ways we use C++ that may differ from what you see elsewhere.
Smart Pointers
link ▶If you actually need pointer semantics, scoped_ptr is great. You should only use std::tr1::shared_ptr under very specific conditions, such as when
object
s need to be held by STL containers. You should never use auto_ptr.
"Smart" pointers are
object
s that act like pointers but have added semantics. When a scoped_ptr is destroyed, for instance, it deletes the
object
it's pointing to. shared_ptr is the same way, but implements reference-counting so only the last pointer to an
object
deletes it.
Generally speaking, we prefer that we design code with clear
object
ownership. The clearest
object
ownership is obtained by using an
object
directly as a field or local variable, without using pointers at all. On the other extreme, by their very definition, reference counted pointers are owned by nobody. The problem with this design is that it is easy to create circular references or other strange conditions that cause an
object
to never be deleted. It is also slow to perform atomic operations every time a value is copied or assigned.
Although they are not recommended, reference counted pointers are sometimes the simplest and most elegant way to solve a problem.
cpplint
link ▶Use cpplint.py to detect style errors.
cpplint.py is a tool that reads a source file and identifies many style errors. It is not perfect, and has both false positives and false negatives, but it is still a valuable tool. False positives can be ignored by putting // NOLINT at the end of the line.
Some projects have instructions on how to run cpplint.py from their project tools. If the project you are contributing to does not, you can download cpplint.py separately.
Other C++ Features
Reference Arguments
link ▶All parameters passed by reference must be labeled const.
Definition:
In C, if a
function
needs to modify a variable, the parameter must use a pointer, eg int foo(int *pval). In C++, the
function
can alternatively declare a reference parameter: int foo(int &val).
Pros:
Defining a parameter as reference avoids ugly code like (*pval)++. Necessary for some applications like copy constructors. Makes it clear, unlike with pointers, that NULL is not a possible value.
Cons:
References can be confusing, as they have value syntax but pointer semantics.
Decision:
Within
function
parameter lists all references must be const:
void Foo(const string &in, string *out);
In fact it is a very strong convention in Google code that input arguments are values or const references while output arguments are pointers. Input parameters may be const pointers, but we never allow non-const reference parameters.
One case when you might want an input parameter to be a const pointer is if you want to emphasize that the argument is not copied, so it must exist for the lifetime of the
object
; it is usually best to
document
this in comments as well. STL adapters such as bind2nd and mem_fun do not permit reference parameters, so you must declare
function
s with pointer parameters in these cases, too.
Function
Overloading
link ▶Use overloaded
function
s (including constructors) only if a reader looking at a call site can get a good idea of what is happening without having to first figure out exactly which overload is being called.
Definition:
You may write a
function
that takes a const string& and overload it with another that takes const char*.
class MyClass {
public:
void Analyze(const string &text);
void Analyze(const char *text, size_t textlen);
Pros:
Overloading can make code more intuitive by allowing an identically-named
function
to take different arguments. It may be necessary for templatized code, and it can be convenient for Visitors.
Cons:
If a
function
is overloaded by the argument types alone, a reader may have to understand C++'s complex matching rules in order to tell what's going on. Also many people are confused by the semantics of inheritance if a derived class overrides only some of the variants of a
function
.
Decision:
If you want to overload a
function
, consider qualifying the name with some information about the arguments, e.g., AppendString(), AppendInt() rather than just Append().
Default Arguments
link ▶We do not allow default
function
parameters, except in a few uncommon situations explained below.
Pros:
Often you have a
function
that uses lots of default values, but occasionally you want to override the defaults. Default parameters allow an easy way to do this without having to define many
function
s for the rare exceptions.
Cons:
People often figure out how to use an API by looking at existing code that uses it. Default parameters are more difficult to maintain because copy-and-paste from previous code may not reveal all the parameters. Copy-and-pasting of code segments can cause major problems when the default arguments are not appropriate for the new code.
Decision:
Except as described below, we require all arguments to be explicitly specified, to force programmers to consider the API and the values they are passing for each argument rather than silently accepting defaults they may not be aware of.
One specific exception is when default arguments are used to simulate variable-length argument lists.
// Support up to 4 params by using a default empty AlphaNum.
string StrCat(const AlphaNum &a,
const AlphaNum &b = gEmptyAlphaNum,
const AlphaNum &c = gEmptyAlphaNum,
const AlphaNum &d = gEmptyAlphaNum);
Variable-Length Arrays and alloca()
link ▶We do not allow variable-length arrays or alloca().
Pros:
Variable-length arrays have natural-looking syntax. Both variable-length arrays and alloca() are very efficient.
Cons:
Variable-length arrays and alloca are not part of Standard C++. More importantly, they allocate a data-dependent amount of stack space that can trigger difficult-to-find memory overwriting bugs: "It ran fine on my machine, but dies mysteriously in production".
Decision:
Use a safe allocator instead, such as scoped_ptr/scoped_array.
Friends
link ▶We allow use of friend classes and
function
s, within reason.
Friends should usually be defined in the same file so that the reader does not have to look in another file to find uses of the private members of a class. A common use of friend is to have a FooBuilder class be a friend of Foo so that it can construct the inner state of Foo correctly, without exposing this state to the world. In some cases it may be useful to make a unittest class a friend of the class it tests.
Friends extend, but do not break, the encapsulation boundary of a class. In some cases this is better than making a member public when you want to give only one other class access to it. However, most classes should interact with other classes solely through their public members.
Exceptions
link ▶We do not use C++ exceptions.
Pros:
Exceptions allow higher levels of an application to decide how to handle "can't happen" failures in deeply nested
function
s, without the obscuring and error-prone bookkeeping of error codes.
Exceptions are used by most other modern languages. Using them in C++ would make it more consistent with Python, Java, and the C++ that others are familiar with.
Some third-party C++ libraries use exceptions, and turning them off internally makes it harder to integrate with those libraries.
Exceptions are the only way for a constructor to fail. We can simulate this with a factory
function
or an Init() method, but these require heap allocation or a new "invalid" state, respectively.
Exceptions are really handy in testing frameworks.
Cons:
When you add a throw statement to an existing
function
, you must examine all of its transitive callers. Either they must make at least the basic exception safety guarantee, or they must never catch the exception and be happy with the program terminating as a result. For instance, if f() calls g() calls h(), and h throws an exception that f catches, g has to be careful or it may not clean up properly.
More generally, exceptions make the control flow of programs difficult to evaluate by looking at code:
function
s may return in places you don't expect. This causes maintainability and debugging difficulties. You can minimize this cost via some rules on how and where exceptions can be used, but at the cost of more that a developer needs to know and understand.
Exception safety requires both RAII and different coding practices. Lots of supporting machinery is needed to make writing correct exception-safe code easy. Further, to avoid requiring readers to understand the entire call graph, exception-safe code must isolate logic that writes to persistent state into a "commit" phase. This will have both benefits and costs (perhaps where you're forced to obfuscate code to isolate the commit). Allowing exceptions would force us to always pay those costs even when they're not worth it.
Turning on exceptions adds data to each binary produced, increasing compile time (probably slightly) and possibly increasing address space pressure.
The availability of exceptions may encourage developers to throw them when they are not appropriate or recover from them when it's not safe to do so. For example, invalid user input should not cause exceptions to be thrown. We would need to make the style guide even longer to
document
these restrictions!
Decision:
On their face, the benefits of using exceptions outweigh the costs, especially in new projects. However, for existing code, the introduction of exceptions has implications on all dependent code. If exceptions can be propagated beyond a new project, it also becomes problematic to integrate the new project into existing exception-free code. Because most existing C++ code at Google is not prepared to deal with exceptions, it is comparatively difficult to adopt new code that generates exceptions.
Given that Google's existing code is not exception-tolerant, the costs of using exceptions are somewhat greater than the costs in a new project. The conversion process would be slow and error-prone. We don't believe that the available alternatives to exceptions, such as error codes and assertions, introduce a significant burden.
Our advice against using exceptions is not predicated on philosophical or moral grounds, but practical ones. Because we'd like to use our open-source projects at Google and it's difficult to do so if those projects use exceptions, we need to advise against exceptions in Google open-source projects as well. Things would probably be different if we had to do it all over again from scratch.
There is an exception to this rule (no pun intended) for Windows code.
Run-Time Type Information (RTTI)
link ▶We do not use Run Time Type Information (RTTI).
Definition:
RTTI allows a programmer to query the C++ class of an
object
at run time.
Pros:
It is useful in some unittests. For example, it is useful in tests of factory classes where the test has to verify that a newly created
object
has the expected dynamic type.
In rare circumstances, it is useful even outside of tests.
Cons:
A query of type during run-time typically means a design problem. If you need to know the type of an
object
at runtime, that is often an indication that you should reconsider the design of your class.
Decision:
Do not use RTTI, except in unittests. If you find yourself in need of writing code that behaves differently based on the class of an
object
, consider one of the alternatives to querying the type.
Virtual methods are the preferred way of executing different code paths depending on a specific subclass type. This puts the work within the
object
itself.
If the work belongs outside the
object
and instead in some processing code, consider a double-dispatch solution, such as the Visitor design pattern. This allows a facility outside the
object
itself to determine the type of class using the built-in type system.
If you think you truly cannot use those ideas, you may use RTTI. But think twice about it. :-) Then think twice again. Do not hand-implement an RTTI-like workaround. The arguments against RTTI apply just as much to workarounds like class hierarchies with type tags.
Casting
link ▶Use C++ casts like static_cast(). Do not use other cast formats like int y = (int)x; or int y = int(x);.
Definition:
C++ introduced a different cast system from C that distinguishes the types of cast operations.
Pros:
The problem with C casts is the ambiguity of the operation; sometimes you are doing a conversion (e.g., (int)3.5) and sometimes you are doing a cast (e.g., (int)"hello"); C++ casts avoid this. Additionally C++ casts are more visible when searching for them.
Cons:
The syntax is nasty.
Decision:
Do not use C-style casts. Instead, use these C++-style casts.
Use static_cast as the equivalent of a C-style cast that does value conversion, or when you need to explicitly up-cast a pointer from a class to its superclass.
Use const_cast to remove the const qualifier (see const).
Use reinterpret_cast to do unsafe conversions of pointer types to and from integer and other pointer types. Use this only if you know what you are doing and you understand the aliasing issues.
Do not use dynamic_cast except in test code. If you need to know type information at runtime in this way outside of a unittest, you probably have a design flaw.
Streams
link ▶Use streams only for logging.
Definition:
Streams are a replacement for printf() and scanf().
Pros:
With streams, you do not need to know the type of the
object
you are printing. You do not have problems with format strings not matching the argument list. (Though with gcc, you do not have that problem with printf either.) Streams have automatic constructors and destructors that open and close the relevant files.
Cons:
Streams make it difficult to do
function
ality like pread(). Some formatting (particularly the common format string idiom %.*s) is difficult if not impossible to do efficiently using streams without using printf-like hacks. Streams do not support operator reordering (the %1s directive), which is helpful for internationalization.
Decision:
Do not use streams, except where required by a logging interface. Use printf-like routines instead.
There are various pros and cons to using streams, but in this case, as in many other cases, consistency trumps the debate. Do not use streams in your code.
Extended Discussion
There has been debate on this issue, so this explains the reasoning in greater depth. Recall the Only One Way guiding principle: we want to make sure that whenever we do a certain type of I/O, the code looks the same in all those places. Because of this, we do not want to allow users to decide between using streams or using printf plus Read/Write/etc. Instead, we should settle on one or the other. We made an exception for logging because it is a pretty specialized application, and for historical reasons.
Proponents of streams have argued that streams are the obvious choice of the two, but the issue is not actually so clear. For every advantage of streams they point out, there is an equivalent disadvantage. The biggest advantage is that you do not need to know the type of the
object
to be printing. This is a fair point. But, there is a downside: you can easily use the wrong type, and the compiler will not warn you. It is easy to make this kind of mistake without knowing when using streams.
cout << this; // Prints the address
cout << *this; // Prints the contents
The compiler does not generate an error because << has been overloaded. We discourage overloading for just this reason.
Some say printf formatting is ugly and hard to read, but streams are often no better. Consider the following two fragments, both with the same typo. Which is easier to discover?
cerr << "Error connecting to '" <bar()->hostname.first
<< ":" <bar()->hostname.second << ": " <bar()->hostname.first, foo->bar()->hostname.second,
strerror(errno));
And so on and so forth for any issue you might bring up. (You could argue, "Things would be better with the right wrappers," but if it is true for one scheme, is it not also true for the other? Also, remember the goal is to make the language smaller, not add yet more machinery that someone has to learn.)
Either path would yield different advantages and disadvantages, and there is not a clearly superior solution. The simplicity doctrine mandates we settle on one of them though, and the majority decision was on printf + read/write.
Preincrement and Predecrement
link ▶Use prefix form (++i) of the increment and decrement operators with iterators and other template
object
s.
Definition:
When a variable is incremented (++i or i++) or decremented (--i or i--) and the value of the expression is not used, one must decide whether to preincrement (decrement) or postincrement (decrement).
Pros:
When the return value is ignored, the "pre" form (++i) is never less efficient than the "post" form (i++), and is often more efficient. This is because post-increment (or decrement) requires a copy of i to be made, which is the value of the expression. If i is an iterator or other non-scalar type, copying i could be expensive. Since the two types of increment behave the same when the value is ignored, why not just always pre-increment?
Cons:
The tradition developed, in C, of using post-increment when the expression value is not used, especially in for loops. Some find post-increment easier to read, since the "subject" (i) precedes the "verb" (++), just like in English.
Decision:
For simple scalar (non-
object
) values there is no reason to prefer one form and we allow either. For iterators and other template types, use pre-increment.
Use of const
link ▶We strongly recommend that you use const whenever it makes sense to do so.
Definition:
Declared variables and parameters can be preceded by the keyword const to indicate the variables are not changed (e.g., const int foo). Class
function
s can have the const qualifier to indicate the
function
does not change the state of the class member variables (e.g., class Foo { int Bar(char c) const; };).
Pros:
Easier for people to understand how variables are being used. Allows the compiler to do better type checking, and, conceivably, generate better code. Helps people convince themselves of program correctness because they know the
function
s they call are limited in how they can modify your variables. Helps people know what
function
s are safe to use without locks in multi-threaded programs.
Cons:
const is viral: if you pass a const variable to a
function
, that
function
must have const in its prototype (or the variable will need a const_cast). This can be a particular problem when calling library
function
s.
Decision:
const variables, data members, methods and arguments add a level of compile-time type checking; it is better to detect errors as soon as possible. Therefore we strongly recommend that you use const whenever it makes sense to do so:
If a
function
does not modify an argument passed by reference or by pointer, that argument should be const.
Declare methods to be const whenever possible. Accessors should almost always be const. Other methods should be const if they do not modify any data members, do not call any non-const methods, and do not return a non-const pointer or non-const reference to a data member.
Consider making data members const whenever they do not need to be modified after construction.
However, do not go crazy with const. Something like const int * const * const x; is likely overkill, even if it accurately describes how const x is. Focus on what's really useful to know: in this case, const int** x is probably sufficient.
The mutable keyword is allowed but is unsafe when used with threads, so thread safety should be carefully considered first.
Where to put the const
Some people favor the form int const *foo to const int* foo. They argue that this is more readable because it's more consistent: it keeps the rule that const always follows the
object
it's describing. However, this consistency argument doesn't apply in this case, because the "don't go crazy" dictum eliminates most of the uses you'd have to be consistent with. Putting the const first is arguably more readable, since it follows English in putting the "adjective" (const) before the "noun" (int).
That said, while we encourage putting const first, we do not require it. But be consistent with the code around you!
Integer Types
link ▶Of the built-in C++ integer types, the only one used is int. If a program needs a variable of a different size, use a precise-width integer type from , such as int16_t.
Definition:
C++ does not specify the sizes of its integer types. Typically people assume that short is 16 bits, int is 32 bits, long is 32 bits and long long is 64 bits.
Pros:
Uniformity of declaration.
Cons:
The sizes of integral types in C++ can vary based on compiler and architecture.
Decision:
defines types like int16_t, uint32_t, int64_t, etc. You should always use those in preference to short, unsigned long long and the like, when you need a guarantee on the size of an integer. Of the C integer types, only int should be used. When appropriate, you are welcome to use standard types like size_t and ptrdiff_t.
We use int very often, for integers we know are not going to be too big, e.g., loop counters. Use plain old int for such things. You should assume that an int is at least 32 bits, but don't assume that it has more than 32 bits. If you need a 64-bit integer type, use int64_t or uint64_t.
For integers we know can be "big", use int64_t.
You should not use the unsigned integer types such as uint32_t, unless the quantity you are representing is really a bit pattern rather than a number, or unless you need defined twos-complement overflow. In particular, do not use unsigned types to say a number will never be negative. Instead, use assertions for this.
On Unsigned Integers
Some people, including some textbook authors, recommend using unsigned types to represent numbers that are never negative. This is intended as a form of self-
document
ation. However, in C, the advantages of such
document
ation are outweighed by the real bugs it can introduce. Consider:
for (unsigned int i = foo.Length()-1; i >= 0; --i) ...
This code will never terminate! Sometimes gcc will notice this bug and warn you, but often it will not. Equally bad bugs can occur when comparing signed and unsigned variables. Basically, C's type-promotion scheme causes unsigned types to behave differently than one might expect.
So,
document
that a variable is non-negative using assertions. Don't use an unsigned type.
64-bit Portability
link ▶Code should be 64-bit and 32-bit friendly. Bear in mind problems of printing, comparisons, and structure alignment.
printf() specifiers for some types are not cleanly portable between 32-bit and 64-bit systems. C99 defines some portable format specifiers. Unfortunately, MSVC 7.1 does not understand some of these specifiers and the standard is missing a few, so we have to define our own ugly versions in some cases (in the style of the standard include file inttypes.h):
// printf macros for size_t, in the style of inttypes.h
#ifdef _LP64
#define __PRIS_PREFIX "z"
#else
#define __PRIS_PREFIX
#endif
// Use these macros after a % in a printf format string
// to get correct 32/64 bit behavior, like this:
// size_t size = records.size();
// printf("%"PRIuS"\n", size);
#define PRIdS __PRIS_PREFIX "d"
#define PRIxS __PRIS_PREFIX "x"
#define PRIuS __PRIS_PREFIX "u"
#define PRIXS __PRIS_PREFIX "X"
#define PRIoS __PRIS_PREFIX "o"
Type DO NOT use DO use Notes
void * (or any pointer) %lx %p
int64_t %qd, %lld %"PRId64"
uint64_t %qu, %llu, %llx %"PRIu64", %"PRIx64"
size_t %u %"PRIuS", %"PRIxS" C99 specifies %zu
ptrdiff_t %d %"PRIdS" C99 specifies %zd
Note that the PRI* macros expand to independent strings which are concatenated by the compiler. Hence if you are using a non-constant format string, you need to insert the value of the macro into the format, rather than the name. It is still possible, as usual, to include length specifiers, etc., after the % when using the PRI* macros. So, e.g. printf("x = %30"PRIuS"\n", x) would expand on 32-bit Linux to printf("x = %30" "u" "\n", x), which the compiler will treat as printf("x = %30u\n", x).
Remember that sizeof(void *) != sizeof(int). Use intptr_t if you want a pointer-sized integer.
You may need to be careful with structure alignments, particularly for structures being stored on disk. Any class/structure with a int64_t/uint64_t member will by default end up being 8-byte aligned on a 64-bit system. If you have such structures being shared on disk between 32-bit and 64-bit code, you will need to ensure that they are packed the same on both architectures. Most compilers offer a way to alter structure alignment. For gcc, you can use __
attribute
__((packed)). MSVC offers #pragma pack() and __declspec(align()).
Use the LL or ULL suffixes a
这两天在学习python的时候,会搞一些测试代码,有时候控制台就会有“
AttributeError
: '
function
'
object
has no
attribute
'name'”的
报错
,编译器也没
报错
在写flask项目中的蓝图时候出现
AttributeError
: ‘
function
’
object
has no
attribute
'name’bug 仔细查阅确实不是代码的问题,源代码如下
在注册蓝图这里经常会出这种错误,后来查阅资料发现,并不一定是你的代码有问题,而是使用的变量名可能与系统(包括扩展库或者保留字)的重复了,修改变量名之后就可以了。当然,这是在你排除了代码本身没有错误的...
写Python程序时,有时会报
AttributeError
: '
function
'
object
has no
attribute
'name'错误,仔细检查了程序,发现代码并没有错误,例如我写的程序:# coding:utf8from flask import Blueprint, Flaskpro = Blueprint('blue', __name__)@pro.route('/pro')d...
当我们使用django框架时, 出现 ‘
function
’
object
has no
attribute
'
object
s’的错误提示,
解决
办法:
(1)我们首先检查自己的代码是否正确,看是否某个地方字母写错了;
(2)在保证代码正确的基础上,出现这个问题是views.py文件的问题,原因在于view.py文件中定义的函数名和app的名字重复了,给def一个和app名字不一样的函数名,就可以了;...