dom

How to get/set current page URL (which works across space-time-browsers-versions)

自古美人都是妖i 提交于 2020-03-18 10:25:14
问题 I want to get/set URL of the current page upon certain event. It seems there are more than one approach for doing it as mentioned in questions/answers below. Using Jquery Get URL - $(location).attr('href'); Set URL - $(location).attr('href',url); Using JavaScript Get URL - myVar = window.location.href; Set URL - window.location.href = "http://stackoverflow.com"; Which approach works across space-time-browsers-versions? Get current URL in JavaScript? How to redirect to another webpage in

正则表达式/DOM读取xml,php/dom编写xml

怎甘沉沦 提交于 2020-03-18 10:21:47
<?php echo "<br/>====================DOM读取xml===========================<br/>"; $doc = new DOMDocument(); $doc->load( 'books.xml' ); $books = $doc->getElementsByTagName("book"); foreach( $books as $book) { $authors = $book->getElementsByTagName("author"); $author = $authors->item(0)->nodeValue; $publishers = $book->getElementsByTagName("publisher"); $publisher = $publishers->item(0)->nodeValue; $titles = $book->getElementsByTagName( "title" ); $title = $titles->item(0)->nodeValue; echo "$title - $author - $publishern"; } echo "<br/>==============================================================

Can you set multiple attributes with the DOM's setAttribute function?

梦想与她 提交于 2020-03-18 07:26:33
问题 Let's say I wanted to create an input element using the DOM. Instead of doing something like this var input = document.createElement("input"); input.setAttribute("class", "my-class"); input.setAttribute("type", "checkbox"); input.setAttribute("checked", "checked"); Is there a DRYer way to write these three lines of code into one line. I know you could do something like this var attributes = ["class", "type", "checked"]; var values = ["my-class", "checkbox", "checked"]; for (var i = 0; i <

Can you set multiple attributes with the DOM's setAttribute function?

做~自己de王妃 提交于 2020-03-18 07:26:12
问题 Let's say I wanted to create an input element using the DOM. Instead of doing something like this var input = document.createElement("input"); input.setAttribute("class", "my-class"); input.setAttribute("type", "checkbox"); input.setAttribute("checked", "checked"); Is there a DRYer way to write these three lines of code into one line. I know you could do something like this var attributes = ["class", "type", "checked"]; var values = ["my-class", "checkbox", "checked"]; for (var i = 0; i <

php解析html类库simple_html_dom

て烟熏妆下的殇ゞ 提交于 2020-03-18 06:12:52
本文介绍simple_html_dom,就是用来处理网页,可以写爬虫抓取信息; 背景 :在项目中获得一个很长的html标签的长字符串,需要解析到里面所有的<img>标签的的src地址; 用正则匹配来做比较麻烦,在朋友的推荐下使用simple_html_dom非常方便, 这个组件不光可以解析字符串,也可以直接传入文件地址和网页url; 下面举例: <?php $content = '';//html内容 // header("Content-type: text/html; charset=utf-8"); include('simple_html_dom.php'); $html = new simple_html_dom(); $html->load($content); $img = $html->find('img'); foreach ($img as $value) { $src[] = $value->getAttribute('data-src'); //获取自定义属性要用getAttribute,否则无法取到 } echo "<pre>"; var_dump($src); echo "</pre>"; 1.首先要引入simple_html_dom库。git地址:https://github.com/samacs/simple_html_dom。 2

jQuery DOM操作

时光毁灭记忆、已成空白 提交于 2020-03-18 05:23:32
jQuery DOM操作 标签属性操作 attr() //getAttrbute() setAttrbute() 路径的相对地址 removeAttr() //removeAttibute() <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <img src="./xiaohua.jpg" alt="" > <script src="./libs/jquery-3.3.1.js"></script> <script> $(function () { //attr //获取值 console.log($('img').attr('src')); //./xiaohua.jpg //设置值 $('img').attr('alt','美女'); //./xiaohua.jpg //设置多个标签属性值 $('img').attr({ 'aaa':'美女', 'bbbb':'个哈哈哈' }); //移除 removeAttr() setTimeout(()=>{ //移除单个属性 // $('img').removeAttr('alt'); //移除多个属性 $('img').removeAttr('alt aaa bbbb'); },3000

HTML DOM setTimeout() 方法

馋奶兔 提交于 2020-03-18 04:22:32
转自:http://www.w3school.com.cn/jsref/met_win_settimeout.asp 1.setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式。 1 <html> 2 <head> 3 <script type="text/javascript"> 4 function timedMsg() 5 { 6 var t=setTimeout("alert('5 seconds!')",5000) 7 } 8 </script> 9 </head> 10 11 <body> 12 <form> 13 <input type="button" value="显示计时的消息框!" onClick = "timedMsg()"> 14 </form> 15 <p>点击上面的按钮。5 秒后会显示一个消息框。</p> 16 </body> 17 18 </html> 来源: https://www.cnblogs.com/sharpest/p/6145256.html

【原创】jQuery1.8.2源码解析之jQuery.data

北城余情 提交于 2020-03-18 02:07:51
数据缓存,jQuery现在支持两种: 1. dom元素,数据存储在jQuery.cache中。 2.普通js对象,数据存储在该对象中。 以下是源代码: 1 var rbrace = /^(?:\{.*\}|\[.*\])$/, 2 rmultiDash = /([A-Z])/g; 3 4 // 首先是对jQuery对象自身的扩展 5 jQuery.extend({ 6 // 即jQuery.cache,负责存储dom元素的缓存数据 7 cache: {}, 8 9 // removeData时,缓存的数据被清除,返回的当时对应的id,以便再利用 10 deletedIds: [], 11 12 // Please use with caution 13 // 将数据存储到jQuery.cache中时,需要唯一id,用它来维护 14 uuid: 0, 15 16 // Unique for each copy of jQuery on the page 17 // Non-digits removed to match rinlinejQuery 18 // 内部key(随即生成),之后会作为key添加到dom的属性集中,而key对应的value则是该dom对应的缓存对象 19 expando: "jQuery" + ( jQuery.fn.jquery + Math.random(

jQuery对象数据缓存Cache原理及jQuery.data详解

烂漫一生 提交于 2020-03-18 01:56:18
网上有很多教你怎么使用jQuery.data(..)来实现数据缓存,但有两个用户经常使用的data([key],[value])和jQuery.data(element,[key],[value])几乎没有什么文章说清楚它们两的区别,所以我用到了,研究下分享给大家。 $("").data([key],[value])与jQuery.data(element,[key],[value])的区别 这两个函数都是用来在元素上存放数据也就平时所说的数据缓存,都返回jQuery对象,当时我分别在使用它俩的时候真的吓我一跳,区别可大了,真是不用不知道,一用吓一跳。看例子先吧,后再根据源代码分析。 Js代码 <div id= "test2" onclick= "test()">test2</div> <div id= "abc3" onclick= "test()">test3</div> <div id= "test" onclick= "test()">test</div> <p id= "ttt">aaaa</p> <script> $(document).ready( function(){ $( "#test").click( function(){ alert( "JQUERY"); var e=$( "div"); //定义了两jquery对象 var w=$( "div"); /

DOM节点操作

只谈情不闲聊 提交于 2020-03-18 01:16:06
DOM中有一个非常重要的功能,就是节点模型,也就是DOM中的“M” 。页面中的元素结构就是通过这种节点模型来互相对应着的,我们只需要通过这些节点关系,可以创建、 插入、替换、克隆、删除等等一些列的元素操作。 创建节点 为了使页面更加智能化,有时我们想动态的在html结构页面添加一个元素标签,那么在插入之前首先要做的动作就是:创建节点。 html代码如下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>DOM节点操作</title> <script type="text/javascript" src="jquery-1.12.3.js"></script> <script type="text/javascript" src="demo.js"></script> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body> </body> </html> 创建一个节点: var box = $("<div id='box'>节点</div>"); //创建节点 将节点插入到<body>元素内部: $("body").append(box); //插入节点 插入节点 在创建节点的过程中,其实我们已经演示怎么通过.append