- 论坛徽章:
- 0
|
jQ.Mobi源代码 2。。。。 - 351. return this;
- 352. },
- 353. attr: function(attr, value) {
- 354. if (this.length === 0)
- 355. return undefined;
- 356. if (value === undefined && !$.isObject(attr)) {
- 357. var val = this[0].getAttribute(attr);
- 358.
- 359. try {
- 360. val = JSON.parse(val);
- 361. } catch (e) {
- 362. }
- 363. return val;
- 364. }
- 365. value = $.isArray(value) || $.isObject(value) ? JSON.stringify(value) : value;
- 366. for (var i = 0; i < this.length; i++) {
- 367. if ($.isObject(attr)) {
- 368. for (var key in attr)
- 369. {
- 370. if (value == null && value !== undefined)
- 371. this[i].removeAttribute(key);
- 372. else
- 373. this[i].setAttribute(key, attr[key]);
- 374. }
- 375. }
- 376. else
- 377. if (value == null && value !== undefined)
- 378. this[i].removeAttribute(attr);
- 379. else
- 380. this[i].setAttribute(attr, value);
- 381. }
- 382. return this;
- 383. },
- 384. removeAttr: function(attr) {
- 385. var that = this;
- 386. for (var i = 0; i < this.length; i++) {
- 387. attr.split(/\s+/g).forEach(function(param) {
- 388. that[i].removeAttribute(param);
- 389. });
- 390. }
- 391. return this;
- 392. },
- 393. remove: function(selector) {
- 394. var elems = $(this).filter(selector);
- 395. for (var i = 0; i < elems.length; i++) {
- 396. elems[i].parentNode.removeChild(elems[i]);
- 397. }
- 398. return elems;
- 399. },
- 400. addClass: function(name) {
- 401. for (var i = 0; i < this.length; i++) {
- 402. var cls = this[i].className;
- 403. var classList = [];
- 404. var that = this;
- 405. name.split(/\s+/g).forEach(function(cname) {
- 406. if (!that.hasClass(cname, that[i]))
- 407. classList.push(cname);
- 408. });
- 409.
- 410. this[i].className += (cls ? " " : "") + classList.join(" ");
- 411. this[i].className = this[i].className.trim();
- 412. }
- 413. return this;
- 414. },
- 415. removeClass: function(name) {
- 416. for (var i = 0; i < this.length; i++) {
- 417. if (name == undefined) {
- 418. this[i].className = '';
- 419. return this;
- 420. }
- 421. var classList = this[i].className;
- 422. name.split(/\s+/g).forEach(function(cname) {
- 423. classList = classList.replace(classRE(cname), "");
- 424. });
- 425. if (classList.length > 0)
- 426. this[i].className = classList.trim();
- 427. else
- 428. this[i].className = "";
- 429. }
- 430. return this;
- 431. },
- 432. hasClass: function(name, element) {
- 433. if (this.length === 0)
- 434. return false;
- 435. if (!element)
- 436. element = this[0];
- 437. return classRE(name).test(element.className);
- 438. },
- 439. bind: function(event, callback) {
- 440. if (event === "" || event == undefined)
- 441. return;
- 442. for (var i = 0; i < this.length; i++) {
- 443. (function(obj) {
- 444.
- 445. var id = obj._eventID ? obj._eventID : _eventID++;
- 446. obj._eventID = id;
- 447. var that = obj;
- 448. event.split(/\s+/g).forEach(function(name) {
- 449. var prxFn = function(event) {
- 450. event.originalEvent = event; //for backwards compatibility with jQuery...leh sigh
- 451. var result = callback.call(that, event);
- 452. if (result === false)
- 453. event.preventDefault();
- 454. return result;
- 455. };
- 456. eventHandlers[id + "_" + name] = prxFn;
- 457. obj.addEventListener(name, prxFn, false);
- 458. });
- 459. })(this[i]);
- 460.
- 461. }
- 462. return this;
- 463. },
- 464. unbind: function(event) {
- 465. if (event === "" || event == undefined)
- 466. return;
- 467. for (var i = 0; i < this.length; i++) {
- 468. (function(obj) {
- 469. var id = obj._eventID;
- 470. var that = obj;
- 471. event.split(/\s+/g).forEach(function(name) {
- 472. if (eventHandlers[id + "_" + name]) {
- 473. var prxFn = eventHandlers[id + "_" + name];
- 474. delete eventHandlers[id + "_" + name];
- 475. that.removeEventListener(name, prxFn, false);
- 476. }
- 477. });
- 478. })(this[i]);
- 479. }
- 480.
- 481. return this;
- 482. },
- 483. trigger: function(event, data) {
- 484. if (this.length === 0)
- 485. return this;
- 486. if (typeof (event) === "string") {
- 487. var evtName = event;
- 488. var newEvent = document.createEvent("Event");
- 489. newEvent.type = evtName;
- 490. newEvent.target = this[0];
- 491. newEvent.initEvent(evtName, false, true);
- 492. }
- 493. else
- 494. var newEvent = event;
- 495. newEvent.data = data;
- 496. this[0].dispatchEvent(newEvent);
- 497. return this;
- 498. },
- 499. append: function(element, insert) {
- 500. if (element && element.length != undefined && element.length === 0)
- 501. return this;
- 502. if ($.isArray(element) || $.isObject(element))
- 503. element = $(element);
- 504. var i;
- 505.
- 506. for (i = 0; i < this.length; i++) {
- 507. if (element.length && typeof element != "string") {
- 508. element = $(element);
- 509. for (var j = 0; j < element.length; j++)
- 510. insert != undefined ? this[i].insertBefore(element[j], this[i].firstChild) : this[i].appendChild(element[j]);
- 511. }
- 512. else {
- 513. var obj = $(element).get();
- 514.
- 515. if (obj == undefined || obj.length == 0) {
- 516. obj = document.createTextNode(element);
- 517. }
- 518. if (obj.nodeName != undefined && obj.nodeName.toLowerCase() == "script" && (!obj.type || obj.type.toLowerCase() === 'text/javascript')) {
- 519. window.eval(obj.innerHTML);
- 520. }
- 521.
- 522. else
- 523. insert != undefined ? this[i].insertBefore(obj, this[i].firstChild) : this[i].appendChild(obj);
- 524. }
- 525. }
- 526. return this;
- 527. },
- 528. prepend: function(element) {
- 529. return this.append(element, 1);
- 530. },
- 531. get: function(index) {
- 532. index = index == undefined ? 0 : index;
- 533. if (index < 0)
- 534. index += this.length;
- 535. return (this[index]) ? this[index] : undefined;
- 536. },
- 537. offset: function() {
- 538. if (this.length === 0)
- 539. return undefined;
- 540. var obj = this[0].getBoundingClientRect();
- 541. return {
- 542. left: obj.left + window.pageXOffset,
- 543. top: obj.top + window.pageYOffset,
- 544. width: parseInt(this[0].style.width),
- 545. height: parseInt(this[0].style.height)
- 546. };
- 547. },
- 548. parent: function(selector) {
- 549. if (this.length == 0)
- 550. return undefined;
- 551. var elems = [];
- 552. for (var i = 0; i < this.length; i++)
- 553. {
- 554. if (this[i].parentNode)
- 555. elems.push(this[i].parentNode);
- 556. }
- 557. return this.setupOld($(unique(elems)).filter(selector));
- 558. },
- 559. children: function(selector) {
- 560.
- 561. if (this.length == 0)
- 562. return undefined;
- 563. var elems = [];
- 564. for (var i = 0; i < this.length; i++)
- 565. {
- 566. elems = elems.concat(siblings(this[i].firstChild));
- 567. }
- 568. return this.setupOld($((elems)).filter(selector));
- 569.
- 570. },
- 571. siblings: function(selector) {
- 572. if (this.length == 0)
- 573. return undefined;
- 574. var elems = [];
- 575. for (var i = 0; i < this.length; i++)
- 576. {
- 577. if (this[i].parentNode)
- 578. elems = elems.concat(siblings(this[i].parentNode.firstChild, this[i]));
- 579. }
- 580. return this.setupOld($(elems).filter(selector));
- 581. },
- 582. closest: function(selector, context) {
- 583. if (this.length == 0)
- 584. return undefined;
- 585. var elems = [], cur = this[0];
- 586.
- 587. var start = $(selector, context);
- 588. if (start.length == 0)
- 589. return $();
- 590. while (cur && start.indexOf(cur) == -1) {
- 591. cur = cur !== context && cur !== document && cur.parentNode;
- 592. }
- 593. return $(cur);
- 594.
- 595. },
- 596. filter: function(selector)
- 597. {
- 598. if (this.length == 0)
- 599. return undefined;
- 600.
- 601. if (selector == undefined)
- 602. return this;
- 603. var elems = [];
- 604. for (var i = 0; i < this.length; i++) {
- 605. var val = this[i];
- 606. if (val.parentNode && $(selector, val.parentNode).indexOf(val) >= 0)
- 607. elems.push(val);
- 608. }
- 609. return this.setupOld($(unique(elems)));
- 610. },
- 611. not: function(selector) {
- 612. if (this.length == 0)
- 613. return undefined;
- 614. var elems = [];
- 615. for (var i = 0; i < this.length; i++) {
- 616. var val = this[i];
- 617. if (val.parentNode && $(selector, val.parentNode).indexOf(val) == -1)
- 618. elems.push(val);
- 619. }
- 620. return this.setupOld($(unique(elems)));
- 621. },
- 622. data: function(key, value) {
- 623. return this.attr('data-' + key, value);
- 624. },
- 625. end: function() {
- 626. return this.oldElement != undefined ? this.oldElement : $();
- 627. },
- 628. clone: function(deep) {
- 629. deep = deep === false ? false : true;
- 630. if (this.length == 0)
- 631. return undefined;
- 632. var elems = [];
- 633. for (var i = 0; i < this.length; i++)
- 634. {
- 635. elems.push(this[i].cloneNode(deep));
- 636. }
- 637.
- 638. return $(elems);
- 639. },
- 640. size: function() {
- 641. return this.length;
- 642. }
- 643.
- 644. };
- 645.
- 646.
- 647. /* AJAX functions */
- 648.
- 649. function empty() {
- 650. }
- 651. var ajaxSettings = {
- 652. type: 'GET',
- 653. beforeSend: empty,
- 654. success: empty,
- 655. error: empty,
- 656. complete: empty,
- 657. context: undefined,
- 658. timeout: 0
- 659. };
- 660.
- 661. $.jsonP = function(options) {
- 662. var callbackName = 'jsonp_callback' + (++_jsonPID);
- 663. var abortTimeout = "",
- 664. context;
- 665. var script = document.createElement("script");
- 666. var abort = function() {
- 667. $(script).remove();
- 668. if (window[callbackName])
- 669. window[callbackName] = empty;
- 670. };
- 671. window[callbackName] = function(data) {
- 672. clearTimeout(abortTimeout);
- 673. $(script).remove();
- 674. delete window[callbackName];
- 675. options.success.call(context, data);
- 676. };
- 677. script.src = options.url.replace(/=\?/, '=' + callbackName);
- 678. $('head').append(script);
- 679. if (options.timeout > 0)
- 680. abortTimeout = setTimeout(function() {
- 681. options.error.call(context, "", 'timeout');
- 682. }, options.timeout);
- 683. return {};
- 684. };
- 685.
- 686. $.ajax = function(opts) {
- 687. var xhr;
- 688. try {
- 689. xhr = new window.XMLHttpRequest();
- 690. var settings = opts || {};
- 691. for (var key in ajaxSettings) {
- 692. if (!settings[key])
- 693. settings[key] = ajaxSettings[key];
- 694. }
- 695.
- 696. if (!settings.url)
- 697. settings.url = window.location;
- 698. if (!settings.contentType)
- 699. settings.contentType = "application/x-www-form-urlencoded";
- 700. if (!settings.headers)
复制代码 |
|