虽然它不是库的重点,但 htmx 确实提供了一个小型辅助方法 API,主要用于 扩展开发 或与 events 配合使用。
¥While it is not a focus of the library, htmx does provide a small API of helper methods, intended mainly for extension development or for working with events.
hyperscript 项目旨在为基于 htmx 的应用提供更广泛的脚本支持。
¥The hyperscript project is intended to provide more extensive scripting support for htmx-based applications.
htmx.addClass()
¥Method - htmx.addClass()
此方法将一个类添加到给定的元素。
¥This method adds a class to the given element.
¥Parameters
elt
- 要将类添加到的元素
¥elt
- the element to add the class to
class
- 要添加的类
¥class
- the class to add
or
elt
- 要将类添加到的元素
¥elt
- the element to add the class to
class
- 要添加的类
¥class
- the class to add
delay
- 添加类之前的延迟(以毫秒为单位)
¥delay
- delay (in milliseconds ) before class is added
¥Example
// add the class 'myClass' to the element with the id 'demo'
htmx.addClass(htmx.find('#demo'), 'myClass');
// add the class 'myClass' to the element with the id 'demo' after 1 second
htmx.addClass(htmx.find('#demo'), 'myClass', 1000);
htmx.ajax()
¥Method - htmx.ajax()
发出 htmx 样式的 AJAX 请求。此方法返回一个 Promise,因此可以在将内容插入 DOM 后执行回调。
¥Issues an htmx-style AJAX request. This method returns a Promise, so a callback can be executed after the content has been inserted into the DOM.
¥Parameters
verb
- ‘GET’、‘POST’ 等
¥verb
- ‘GET’, ‘POST’, etc.
path
- 进行 AJAX 的 URL 路径
¥path
- the URL path to make the AJAX
element
- 要定位的元素(默认为 body
)
¥element
- the element to target (defaults to the body
)
or
verb
- ‘GET’、‘POST’ 等
¥verb
- ‘GET’, ‘POST’, etc.
path
- 进行 AJAX 的 URL 路径
¥path
- the URL path to make the AJAX
selector
- 目标的选择器
¥selector
- a selector for the target
or
verb
- ‘GET’、‘POST’ 等
¥verb
- ‘GET’, ‘POST’, etc.
path
- 进行 AJAX 的 URL 路径
¥path
- the URL path to make the AJAX
context
- 包含以下任何内容的上下文对象
¥context
- a context object that contains any of the following
source
- 请求的源元素,影响请求的 hx-*
属性将针对该元素及其祖级进行解析
¥source
- the source element of the request, hx-*
attrs which affect the request will be resolved against that element and its ancestors
event
- “triggered” 请求的事件
¥event
- an event that “triggered” the request
handler
- 处理响应 HTML 的回调
¥handler
- a callback that will handle the response HTML
target
- 将响应交换到的目标
¥target
- the target to swap the response into
swap
- 响应将如何相对于目标进行交换
¥swap
- how the response will be swapped in relative to the target
values
- 随请求提交的值
¥values
- values to submit with the request
headers
- 与请求一起提交的标头
¥headers
- headers to submit with the request
select
- 允许你从响应中选择要交换的内容
¥select
- allows you to select the content you want swapped from a response
¥Example
// issue a GET to /example and put the response HTML into #myDiv
htmx.ajax('GET', '/example', '#myDiv')
// issue a GET to /example and replace #myDiv with the response
htmx.ajax('GET', '/example', {target:'#myDiv', swap:'outerHTML'})
// execute some code after the content has been inserted into the DOM
htmx.ajax('GET', '/example', '#myDiv').then(() => {
// this code will be executed after the 'htmx:afterOnLoad' event,
// and before the 'htmx:xhr:loadend' event
console.log('Content inserted successfully!');
});
htmx.closest()
¥Method - htmx.closest()
查找给定元素父级中最接近的匹配元素,包括元素
¥Finds the closest matching element in the given elements parentage, inclusive of the element
¥Parameters
elt
- 要查找选择器的元素来自
¥elt
- the element to find the selector from
selector
- 要查找的选择器
¥selector
- the selector to find
¥Example
// find the closest enclosing div of the element with the id 'demo'
htmx.closest(htmx.find('#demo'), 'div');
htmx.config
¥Property - htmx.config
保存 htmx 在运行时使用的配置的属性。
¥A property holding the configuration htmx uses at runtime.
请注意,使用 meta 标签 是设置这些属性的首选机制。
¥Note that using a meta tag is the preferred mechanism for setting these properties.
¥Properties
attributesToSettle:["class", "style", "width", "height"]
- 字符串数组:在解决阶段要解决的属性
¥attributesToSettle:["class", "style", "width", "height"]
- array of strings: the attributes to settle during the settling phase
refreshOnHistoryMiss:false
- 布尔值:如果设置为 true
,htmx 将在历史记录未命中时发出整页刷新,而不是使用 AJAX 请求
¥refreshOnHistoryMiss:false
- boolean: if set to true
htmx will issue a full page refresh on history misses rather than use an AJAX request
defaultSettleDelay:20
- int:完成内容交换和稳定属性之间的默认延迟
¥defaultSettleDelay:20
- int: the default delay between completing the content swap and settling attributes
defaultSwapDelay:0
- int:从服务器接收响应和执行交换之间的默认延迟
¥defaultSwapDelay:0
- int: the default delay between receiving a response from the server and doing the swap
defaultSwapStyle:'innerHTML'
- 字符串:如果省略 hx-swap
,则使用默认交换样式
¥defaultSwapStyle:'innerHTML'
- string: the default swap style to use if hx-swap
is omitted
historyCacheSize:10
- int:为支持历史记录而在 localStorage
中保留的页面数
¥historyCacheSize:10
- int: the number of pages to keep in localStorage
for history support
historyEnabled:true
- 布尔值:是否使用历史记录
¥historyEnabled:true
- boolean: whether or not to use history
includeIndicatorStyles:true
- 布尔值:如果为 true,则 htmx 将在页面中注入少量 CSS,以使指示器不可见,除非存在 htmx-indicator
类
¥includeIndicatorStyles:true
- boolean: if true, htmx will inject a small amount of CSS into the page to make indicators invisible unless the htmx-indicator
class is present
indicatorClass:'htmx-indicator'
- 字符串:请求正在进行时放置在指示器上的类
¥indicatorClass:'htmx-indicator'
- string: the class to place on indicators when a request is in flight
requestClass:'htmx-request'
- 字符串:请求正在进行时放置在触发元素上的类
¥requestClass:'htmx-request'
- string: the class to place on triggering elements when a request is in flight
addedClass:'htmx-added'
- 字符串:htmx 已添加到 DOM 的元素上临时放置的类
¥addedClass:'htmx-added'
- string: the class to temporarily place on elements that htmx has added to the DOM
settlingClass:'htmx-settling'
- 字符串:htmx 处于稳定阶段时放置在目标元素上的类
¥settlingClass:'htmx-settling'
- string: the class to place on target elements when htmx is in the settling phase
swappingClass:'htmx-swapping'
- 字符串:htmx 处于交换阶段时放置在目标元素上的类
¥swappingClass:'htmx-swapping'
- string: the class to place on target elements when htmx is in the swapping phase
allowEval:true
- 布尔值:允许在 htmx 中使用类似 eval 的功能,以启用 hx-vars
、触发条件和脚本标记评估。可以设置为 false
以实现 CSP 兼容性。
¥allowEval:true
- boolean: allows the use of eval-like functionality in htmx, to enable hx-vars
, trigger conditions & script tag evaluation. Can be set to false
for CSP compatibility.
allowScriptTags:true
- 布尔值:允许在新内容中评估脚本标记
¥allowScriptTags:true
- boolean: allows script tags to be evaluated in new content
inlineScriptNonce:''
- 字符串:要添加到内联脚本的 nonce
¥inlineScriptNonce:''
- string: the nonce to add to inline scripts
inlineStyleNonce:''
- 字符串:要添加到内联样式的 nonce
¥inlineStyleNonce:''
- string: the nonce to add to inline styles
withCredentials:false
- 布尔值:允许使用 cookie、授权标头或 TLS 客户端证书等凭证进行跨站点访问控制请求
¥withCredentials:false
- boolean: allow cross-site Access-Control requests using credentials such as cookies, authorization headers or TLS client certificates
timeout:0
- int:请求在自动终止之前可以花费的毫秒数
¥timeout:0
- int: the number of milliseconds a request can take before automatically being terminated
wsReconnectDelay:'full-jitter'
- 字符串/函数:事件代码 Abnormal Closure
、Service Restart
或 getWebSocketReconnectDelay
意外断开连接后重新连接的默认 getWebSocketReconnectDelay
实现 Try Again Later
¥wsReconnectDelay:'full-jitter'
- string/function: the default implementation of getWebSocketReconnectDelay
for reconnecting after unexpected connection loss by the event code Abnormal Closure
, Service Restart
or Try Again Later
wsBinaryType:'blob'
- 字符串:通过 WebSocket 连接接收的 二进制数据的类型
¥wsBinaryType:'blob'
- string: the the type of binary data being received over the WebSocket connection
disableSelector:"[hx-disable], [data-hx-disable]"
- 字符串数组:htmx 将不会处理具有此属性或其父级的元素
¥disableSelector:"[hx-disable], [data-hx-disable]"
- array of strings: htmx will not process elements with this attribute on it or a parent
disableInheritance:false
- 布尔值:如果将其设置为 true
,则属性的继承将完全禁用,你可以使用 hx-inherit 属性明确指定继承。
¥disableInheritance:false
- boolean: If it is set to true
, the inheritance of attributes is completely disabled and you can explicitly specify the inheritance with the hx-inherit attribute.
scrollBehavior:'instant'
- 字符串:将 show 修饰符与 hx-swap
一起使用时的滚动行为。允许的值为 instant
(滚动应在一次跳转中立即发生)、smooth
(滚动应流畅地进行动画处理)和 auto
(滚动行为由 scroll-behavior 的计算值决定)。
¥scrollBehavior:'instant'
- string: the scroll behavior when using the show modifier with hx-swap
. The allowed values are instant
(scrolling should happen instantly in a single jump), smooth
(scrolling should animate smoothly) and auto
(scroll behavior is determined by the computed value of scroll-behavior).
defaultFocusScroll:false
- 布尔值:如果焦点元素应该滚动到视图中,则可以使用 focus-scroll swap 修饰符进行覆盖
¥defaultFocusScroll:false
- boolean: if the focused element should be scrolled into view, can be overridden using the focus-scroll swap modifier
getCacheBusterParam:false
- 布尔值:如果设置为 true,htmx 将以 org.htmx.cache-buster=targetElementId
格式将目标元素附加到 GET
请求
¥getCacheBusterParam:false
- boolean: if set to true htmx will append the target element to the GET
request in the format org.htmx.cache-buster=targetElementId
globalViewTransitions:false
- 布尔值:如果设置为 true
,htmx 将在交换新内容时使用 查看转换 API。
¥globalViewTransitions:false
- boolean: if set to true
, htmx will use the View Transition API when swapping in new content.
methodsThatUseUrlParams:["get", "delete"]
- 字符串数组:htmx 将通过在 URL 中编码其参数(而不是请求主体)来格式化使用这些方法的请求
¥methodsThatUseUrlParams:["get", "delete"]
- array of strings: htmx will format requests with these methods by encoding their parameters in the URL, not the request body
selfRequestsOnly:true
- 布尔值:是否仅允许 AJAX 请求与当前文档相同的域
¥selfRequestsOnly:true
- boolean: whether to only allow AJAX requests to the same domain as the current document
ignoreTitle:false
- 布尔值:如果设置为 true
,当在新的内容中发现 title
标签时,htmx 将不会更新文档的标题
¥ignoreTitle:false
- boolean: if set to true
htmx will not update the title of the document when a title
tag is found in new content
scrollIntoViewOnBoost:true
- 布尔值:是否将增强元素的目标滚动到视口中。如果在提升元素上省略 hx-target
,则目标默认为 body
,导致页面滚动到顶部。
¥scrollIntoViewOnBoost:true
- boolean: whether or not the target of a boosted element is scrolled into the viewport. If hx-target
is omitted on a boosted element, the target defaults to body
, causing the page to scroll to the top.
triggerSpecsCache:null
- object:用于存储已评估的触发器规范的缓存,以更多的内存使用为代价提高解析性能。你可以定义一个简单的对象来使用永不清除的缓存,或者使用 代理对象 实现你自己的系统
¥triggerSpecsCache:null
- object: the cache to store evaluated trigger specifications into, improving parsing performance at the cost of more memory usage. You may define a simple object to use a never-clearing cache, or implement your own system using a proxy object
htmx.config.responseHandling:[...]
- HtmxResponseHandlingConfig[]:响应状态代码的默认 响应处理 行为可以在此处配置为交换或错误
¥htmx.config.responseHandling:[...]
- HtmxResponseHandlingConfig[]: the default Response Handling behavior for response status codes can be configured here to either swap or error
htmx.config.allowNestedOobSwaps:true
- 布尔值:是否处理嵌套在主响应元素中的元素的 OOB 交换。参见 嵌套 OOB 交换。
¥htmx.config.allowNestedOobSwaps:true
- boolean: whether to process OOB swaps on elements that are nested within the main response element. See Nested OOB Swaps.
htmx.config.historyRestoreAsHxRequest:true
- 是否通过返回此响应标头将历史记录缓存未命中的整页重新加载请求视为 “HX-Request”。使用 HX-Request 标头选择性地返回部分响应时,应始终禁用此功能
¥htmx.config.historyRestoreAsHxRequest:true
- Whether to treat history cache miss full page reload requests as a “HX-Request” by returning this response header. This should always be disabled when using HX-Request header to optionally return partial responses
¥Example
// update the history cache size to 30
htmx.config.historyCacheSize = 30;
htmx.createEventSource
¥Property - htmx.createEventSource
用于创建新 服务器发送事件 源的属性。可以更新以提供自定义 SSE 设置。
¥A property used to create new Server Sent Event sources. This can be updated to provide custom SSE setup.
¥Value
func(url)
- 接受 URL 字符串并返回新 EventSource
的函数
¥func(url)
- a function that takes a URL string and returns a new EventSource
¥Example
// override SSE event sources to not use credentials
htmx.createEventSource = function(url) {
return new EventSource(url, {withCredentials:false});
};
htmx.createWebSocket
¥Property - htmx.createWebSocket
用于创建新 WebSocket 的属性。这可以更新以提供自定义 WebSocket 设置。
¥A property used to create new WebSocket. This can be updated to provide custom WebSocket setup.
¥Value
func(url)
- 接受 URL 字符串并返回新 WebSocket
的函数
¥func(url)
- a function that takes a URL string and returns a new WebSocket
¥Example
// override WebSocket to use a specific protocol
htmx.createWebSocket = function(url) {
return new WebSocket(url, ['wss']);
};
htmx.defineExtension()
¥Method - htmx.defineExtension()
定义一个新的 htmx extension。
¥Defines a new htmx extension.
¥Parameters
name
- 扩展名称
¥name
- the extension name
ext
- 扩展定义
¥ext
- the extension definition
¥Example
// defines a silly extension that just logs the name of all events triggered
htmx.defineExtension("silly", {
onEvent : function(name, evt) {
console.log("Event " + name + " was triggered!")
}
});
htmx.find()
¥Method - htmx.find()
查找与选择器匹配的元素
¥Finds an element matching the selector
¥Parameters
selector
- 要匹配的选择器
¥selector
- the selector to match
or
elt
- 要在其中查找匹配元素的根元素(包括)
¥elt
- the root element to find the matching element in, inclusive
selector
- 要匹配的选择器
¥selector
- the selector to match
¥Example
// find div with id my-div
var div = htmx.find("#my-div")
// find div with id another-div within that div
var anotherDiv = htmx.find(div, "#another-div")
htmx.findAll()
¥Method - htmx.findAll()
查找与选择器匹配的所有元素
¥Finds all elements matching the selector
¥Parameters
selector
- 要匹配的选择器
¥selector
- the selector to match
or
elt
- 要在其中查找匹配元素的根元素(包括)
¥elt
- the root element to find the matching elements in, inclusive
selector
- 要匹配的选择器
¥selector
- the selector to match
¥Example
// find all divs
var allDivs = htmx.findAll("div")
// find all paragraphs within a given div
var allParagraphsInMyDiv = htmx.findAll(htmx.find("#my-div"), "p")
htmx.logAll()
¥Method - htmx.logAll()
记录所有 htmx 事件,对调试很有用。
¥Log all htmx events, useful for debugging.
¥Example
htmx.logAll();
htmx.logNone()
¥Method - htmx.logNone()
不记录 htmx 事件,如果之前启用了调试器,请调用此方法关闭它。
¥Log no htmx events, call this to turn off the debugger if you previously enabled it.
¥Example
htmx.logNone();
htmx.logger
¥Property - htmx.logger
htmx 用于记录的日志器
¥The logger htmx uses to log with
¥Value
func(elt, eventName, detail)
- 接受元素、eventName 和事件详细信息并记录它的函数
¥func(elt, eventName, detail)
- a function that takes an element, eventName and event detail and logs it
¥Example
htmx.logger = function(elt, event, data) {
if(console) {
console.log("INFO:", event, elt, data);
}
}
htmx.off()
¥Method - htmx.off()
从元素中删除一个事件监听器
¥Removes an event listener from an element
¥Parameters
eventName
- 要从中移除监听器的事件名称
¥eventName
- the event name to remove the listener from
listener
- 要删除的监听器
¥listener
- the listener to remove
or
target
- 要从中移除监听器的元素
¥target
- the element to remove the listener from
eventName
- 要从中移除监听器的事件名称
¥eventName
- the event name to remove the listener from
listener
- 要删除的监听器
¥listener
- the listener to remove
¥Example
// remove this click listener from the body
htmx.off("click", myEventListener);
// remove this click listener from the given div
htmx.off("#my-div", "click", myEventListener)
htmx.on()
¥Method - htmx.on()
向元素添加事件监听器
¥Adds an event listener to an element
¥Parameters
eventName
- 要为其添加监听器的事件名称
¥eventName
- the event name to add the listener for
listener
- 要添加的监听器
¥listener
- the listener to add
options
- 要添加到事件监听器的 options 对象(或 useCapture 布尔值)(可选)
¥options
- an options object (or a useCapture boolean) to add to the event listener (optional)
or
target
- 要将监听器添加到的元素
¥target
- the element to add the listener to
eventName
- 要为其添加监听器的事件名称
¥eventName
- the event name to add the listener for
listener
- 要添加的监听器
¥listener
- the listener to add
options
- 要添加到事件监听器的 options 对象(或 useCapture 布尔值)(可选)
¥options
- an options object (or a useCapture boolean) to add to the event listener (optional)
¥Example
// add a click listener to the body
var myEventListener = htmx.on("click", function(evt){ console.log(evt); });
// add a click listener to the given div
var myEventListener = htmx.on("#my-div", "click", function(evt){ console.log(evt); });
// add a click listener to the given div that should only be invoked once
var myEventListener = htmx.on("#my-div", "click", function(evt){ console.log(evt); }, { once: true });
htmx.onLoad()
¥Method - htmx.onLoad()
为 htmx:load
事件添加回调。这可用于处理新内容,例如使用 javascript 库初始化内容
¥Adds a callback for the htmx:load
event. This can be used to process new content, for example
initializing the content with a javascript library
¥Parameters
callback(elt)
- 调用新加载内容的回调
¥callback(elt)
- the callback to call on newly loaded content
¥Example
htmx.onLoad(function(elt){
MyLibrary.init(elt);
})
htmx.parseInterval()
¥Method - htmx.parseInterval()
解析与 htmx 方式一致的间隔字符串。对于具有时间相关属性的插件很有用。
¥Parses an interval string consistent with the way htmx does. Useful for plugins that have timing-related attributes.
警告:接受 int,后跟 s
或 ms
。所有其他值都使用 parseFloat
¥Caution: Accepts an int followed by either s
or ms
. All other values use parseFloat
¥Parameters
str
- 时间字符串
¥str
- timing string
¥Example
// returns 3000
var milliseconds = htmx.parseInterval("3s");
// returns 3 - Caution
var milliseconds = htmx.parseInterval("3m");
htmx.process()
¥Method - htmx.process()
处理新内容,启用 htmx 行为。如果你有在正常 htmx 请求周期之外添加到 DOM 的内容但仍希望 htmx 属性起作用,这将很有用。
¥Processes new content, enabling htmx behavior. This can be useful if you have content that is added to the DOM outside of the normal htmx request cycle but still want htmx attributes to work.
¥Parameters
elt
- 要处理的元素
¥elt
- element to process
¥Example
document.body.innerHTML = "<div hx-get='/example'>Get it!</div>"
// process the newly added content
htmx.process(document.body);
htmx.remove()
¥Method - htmx.remove()
从 DOM 中删除一个元素
¥Removes an element from the DOM
¥Parameters
elt
- 要删除的元素
¥elt
- element to remove
or
elt
- 要删除的元素
¥elt
- element to remove
delay
- 删除元素之前的延迟(以毫秒为单位)
¥delay
- delay (in milliseconds ) before element is removed
¥Example
// removes my-div from the DOM
htmx.remove(htmx.find("#my-div"));
// removes my-div from the DOM after a delay of 2 seconds
htmx.remove(htmx.find("#my-div"), 2000);
htmx.removeClass()
¥Method - htmx.removeClass()
从给定元素中删除一个类
¥Removes a class from the given element
¥Parameters
elt
- 要从中删除类的元素
¥elt
- element to remove the class from
class
- 要删除的类
¥class
- the class to remove
or
elt
- 要从中删除类的元素
¥elt
- element to remove the class from
class
- 要删除的类
¥class
- the class to remove
delay
- 删除类之前的延迟(以毫秒为单位)
¥delay
- delay (in milliseconds ) before class is removed
¥Example
// removes .myClass from my-div
htmx.removeClass(htmx.find("#my-div"), "myClass");
// removes .myClass from my-div after 6 seconds
htmx.removeClass(htmx.find("#my-div"), "myClass", 6000);
htmx.removeExtension()
¥Method - htmx.removeExtension()
从 htmx 中删除给定的扩展名
¥Removes the given extension from htmx
¥Parameters
name
- 要删除的扩展的名称
¥name
- the name of the extension to remove
¥Example
htmx.removeExtension("my-extension");
htmx.swap()
¥Method - htmx.swap()
执行 HTML 内容的交换(和解决)
¥Performs swapping (and settling) of HTML content
¥Parameters
target
- 交换目标的 HTML 元素或字符串选择器
¥target
- the HTML element or string selector of swap target
content
- 要交换的内容的字符串表示
¥content
- string representation of content to be swapped
swapSpec
- 交换规范,表示来自 hx-swap
的参数
¥swapSpec
- swapping specification, representing parameters from hx-swap
swapStyle
(必需) - 交换样式(innerHTML
、outerHTML
、beforebegin
等)
¥swapStyle
(required) - swapping style (innerHTML
, outerHTML
, beforebegin
etc)
swapDelay
、settleDelay
(数字) - 分别在交换和稳定之前的延迟
¥swapDelay
, settleDelay
(number) - delays before swapping and settling respectively
transition
(bool) - 是否使用 HTML 转换进行交换
¥transition
(bool) - whether to use HTML transitions for swap
ignoreTitle
(bool) - 禁用页面标题更新
¥ignoreTitle
(bool) - disables page title updates
head
(字符串) - 指定 head
标签处理策略(merge
或 append
)。留空以禁用头部处理
¥head
(string) - specifies head
tag handling strategy (merge
or append
). Leave empty to disable head handling
scroll
, scrollTarget
, show
, showTarget
, focusScroll
- 指定交换后的滚动处理
¥scroll
, scrollTarget
, show
, showTarget
, focusScroll
- specifies scroll handling after swap
swapOptions
- 用于交换的其他可选参数
¥swapOptions
- additional optional parameters for swapping
select
- 要交换的内容的选择器(相当于 hx-select
)
¥select
- selector for the content to be swapped (equivalent of hx-select
)
selectOOB
- 要带外交换的内容的选择器(相当于 hx-select-oob
)
¥selectOOB
- selector for the content to be swapped out-of-band (equivalent of hx-select-oob
)
eventInfo
- 要附加到 htmx:afterSwap
和 htmx:afterSettle
元素的对象
¥eventInfo
- an object to be attached to htmx:afterSwap
and htmx:afterSettle
elements
anchor
- 触发滚动的锚元素将在解决时滚动到视图中。提供完全滚动处理的简单替代方案
¥anchor
- an anchor element that triggered scroll, will be scrolled into view on settle. Provides simple alternative to full scroll handling
contextElement
- 作为交换操作上下文的 DOM 元素。目前用于查找为特定元素启用的扩展
¥contextElement
- DOM element that serves as context to swapping operation. Currently used to find extensions enabled for specific element
afterSwapCallback
, afterSettleCallback
- 分别在 swap 和 setter 之后调用的回调函数。不带参数
¥afterSwapCallback
, afterSettleCallback
- callback functions called after swap and settle respectively. Take no arguments
¥Example
// swap #output element inner HTML with div element with "Swapped!" text
htmx.swap("#output", "<div>Swapped!</div>", {swapStyle: 'innerHTML'});
htmx.takeClass()
¥Method - htmx.takeClass()
从其兄弟元素中获取给定的类,以便在其兄弟元素中,只有给定的元素才具有该类。
¥Takes the given class from its siblings, so that among its siblings, only the given element will have the class.
¥Parameters
elt
- 将采用类的元素
¥elt
- the element that will take the class
class
- 要采用的类
¥class
- the class to take
¥Example
// takes the selected class from tab2's siblings
htmx.takeClass(htmx.find("#tab2"), "selected");
htmx.toggleClass()
¥Method - htmx.toggleClass()
在元素上切换给定类
¥Toggles the given class on an element
¥Parameters
elt
- 要切换类的元素
¥elt
- the element to toggle the class on
class
- 要切换的类
¥class
- the class to toggle
¥Example
// toggles the selected class on tab2
htmx.toggleClass(htmx.find("#tab2"), "selected");
htmx.trigger()
¥Method - htmx.trigger()
在元素上触发给定事件
¥Triggers a given event on an element
¥Parameters
elt
- 要触发事件的元素
¥elt
- the element to trigger the event on
name
- 要触发的事件的名称
¥name
- the name of the event to trigger
detail
- 事件的详细信息
¥detail
- details for the event
¥Example
// triggers the myEvent event on #tab2 with the answer 42
htmx.trigger("#tab2", "myEvent", {answer:42});
htmx.values()
¥Method - htmx.values()
返回通过 htmx 值解析机制解析给定元素的输入值
¥Returns the input values that would resolve for a given element via the htmx value resolution mechanism
¥Parameters
elt
- 要解析值的元素
¥elt
- the element to resolve values on
request type
- 请求类型(例如 get
或 post
)非 GET 将包括封闭形式元素的。默认为 post
¥request type
- the request type (e.g. get
or post
) non-GET’s will include the enclosing form of the element.
Defaults to post
¥Example
// gets the values associated with this form
var values = htmx.values(htmx.find("#myForm"));